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 |
Explain about Garbage Collector?
What is jump statement in C++?
Explain the operation of overloading of an assignment operator.
What are function poinetrs? where are they used?
What are c++ redistributables?
. If employee B is the boss of A and C is the boss of B and D is the boss of C and E is the boss of D. Then write a program using the Database such that if an employee name is Asked to Display it also display his bosses with his name. For eg. If C is displayed it should also display D and E with C?
If horse and bird inherit virtual public from animal, do their constructors initialize the animal constructor? If pegasus inherits from both horse and bird, how does it initialize animal’s constructor?
What is data abstraction? How is it different from data encapsulation?
What is the use of volatile variable?
the maximum length of a character constant can be a) 2 b) 1 c) 8
How can we read/write Structures from/to data files?
Why preincrement operator is faster than postincrement?