Given an array of size N in which every number is between 1 and
N, determine if there are any duplicates in it. You are allowed
to destroy the array if you like. [ I ended up giving about 4 or
5 different solutions for this, each supposedly better than the
others ].

Answer Posted / sujan_faith

<!--i have just replaced the same array element with
the value zero but alse we can destroy that element-->>


#include<iostream>
#define size 10
using namespace std;

int main()
{
int a[10]={10,10,10,2,1,1,1,1,1,2};
int j=1;
for(int i=0;i<size;i++)
{
for(int k=i+1;k<size;k++)
{
if(a[i]==a[k]&&a[i]!=0)
{
a[k]=0;
j++;

}
if(k==(size-1)&& a[i]!=0)
cout<<"Value is: "<<a[i]<<" which is repeated "<<j<<" times"<<endl;
}
j=1;
}
system("pause");
}

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a new be used in place of old mallocq? If yes, why?

632


How do you invoke a base member function from a derived class in which you have not overridden that function?

580


Which programming language is best to learn first?

580


Explain the register storage classes in c++.

665


Are there interfaces in c++?

556






What are shallow and deep copy?

592


Do the parentheses after the type name make a difference with new?

640


What is rvalue?

673


Tell me can a pure virtual function have an implementation?

554


Is c++ vector dynamic?

572


How can you tell what shell you are running on unix system?

636


Write a corrected statement in c++ so that the statement will work properly. if (4 < x < 11) y=2*x;

1499


Explain what are the sizes and ranges of the basic c++ data types?

638


Is it possible for a member function to delete the pointer, named this?

608


Differentiate between realloc() and free().

590