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 |
Can you please explain the difference between using macro and inline functions?
How do I run a program in notepad ++?
Describe the setting up of my member functions to avoid overriding by the derived class?
What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00
Why do we use classes in programming?
Is there any difference between int [] a and int a [] in c++?
Which sort is best for the set: 1 2 3 5 4 a) Quick Sort b) Bubble Sort c) Merge Sort
Explain the use of this pointer?
What is the main function c++?
What is the v-ptr?
What is the difference between a copy constructor and an overloaded assignment operator?
4 Answers Belzabar, Citrix, Microsoft, Wipro,
What is the best it certification?