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 / 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 |
Write about an iterator class?
What is the difference between inline functions and macros?
When we use Abstract Class and when we use Interface?where we will implement in real time?
Why cout is used in c++?
Eplain extern keyword?
Evaluate as true or false: !(1 &&0 || !1) a) True b) False c) Invalid statement
What are the syntactic rules to be avoid ambiguity in multiple inheritance?
Are vectors passed by reference c++?
What is the difference between prefix and postfix versions of operator++()?
What is a container class? What are the types of container classes?
How do I tokenize a string in c++?
What is istream c++?