C Code I need to edit the code in the function deleteDuplica

C++ Code

I need to edit the code in the function deleteDuplicates() to reduce the Big O to O(N), meaning, to get to use only one loop instead of two.

The output after the sorting is 1 1 1 2 2 4 6 7 9 9. The output should be 1 0 0 2 0 4 6 7 9 0 after the deleteDuplicates() funtion.

C++ Code

**************************************************************************************

#include<iostream>
#include<string>
#include<stdio.h>

//using namespace std;
int a[] = { 1, 7, 1, 2, 4, 6, 1, 9, 9, 2 };
int size = sizeof(a)/sizeof(a[0]);
void display()
{
   for (int i = 0; i < size; i++)
   {
       printf(\"%d \", a[i]);
   }
   printf(\"\ \");
}

   void insertionSort() {
for(int i = 1; i < size; i++) {
for(int j = i-1; j >= 0; j--){
int temp = a[j];
printf(\"%d > %d ?\ \", a[j], a[j+1]);
if(a[j] > a[j+1]){
a[j] = a[j+1];
a[j+1] = temp;
}
else
break;
}
display();
}
   }

   void deleteDuplicates() {
   for(int i = 0; i < size; i++) {
for(int j = i+1; j >= 0; j--){
if(a[j] == a[j+1]){
a[j+1] = 0;
}
else
break;
}
display();
}

   }

int main()
{

   display();
   insertionSort();
display();
deleteDuplicates();
display();
   return 0;

}

**************************************************************************************

C++ Code

Solution

#include<iostream>
#include<string>
#include<stdio.h>
//using namespace std;
int a[] = { 1, 7, 1, 2, 4, 6, 1, 9, 9, 2 };
int size = sizeof(a)/sizeof(a[0]);
void display()
{
for (int i = 0; i < size; i++)
{
printf(\"%d \", a[i]);
}
printf(\"\ \");
}
void insertionSort() {
for(int i = 1; i < size; i++) {
for(int j = i-1; j >= 0; j--){
int temp = a[j];
printf(\"%d > %d ?\ \", a[j], a[j+1]);
if(a[j] > a[j+1]){
a[j] = a[j+1];
a[j+1] = temp;
}
else
break;
}
display();
}
}
void deleteDuplicates() {
for(int i = size-1; i >= 0; i--) {

if(a[i] == a[i-1])
a[i] = 0;



display();
}
}
int main()
{
display();
insertionSort();
display();
deleteDuplicates();
display();
return 0;
}

C++ Code I need to edit the code in the function deleteDuplicates() to reduce the Big O to O(N), meaning, to get to use only one loop instead of two. The output
C++ Code I need to edit the code in the function deleteDuplicates() to reduce the Big O to O(N), meaning, to get to use only one loop instead of two. The output
C++ Code I need to edit the code in the function deleteDuplicates() to reduce the Big O to O(N), meaning, to get to use only one loop instead of two. The output

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site