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
How do you instruct your compiler to print the contents of the intermediate file showing the effects of the preprocessor?
What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required
Is oops and c++ same?
Explain one-definition rule (odr).
write a corrected statement so that the instruction will work properly. if (4 < x < 11) y = 2 * x;
Why do we use double in c++?
Explain object slicing in c++?
Why main function is special in c++?
what kind of projects are suitable for c and c++
What does ctime() do?
Should I learn c or c++ or c#?
Explain the concept of friend function in c++?
what is software cycle? What is a mission critical system ? What is the important aspect of a real-time system ? Explain the difference between microkernel and macro kernel. Give an example of microkernel.Why paging is used ? Which is the best page replacement algo and Why ? What is software life cycle ? How much time is spent usually in each phases and why Which one do U want to work if selected in Honeywell ? Which are the different types of testing ? What is a distributed system ? Some questions about CSP. Which languages do U know ? What are the differences between Pascal and C. questions from Compiler construction and Lisp. Which are the different computer architecture? What is the requirement in MIMD ? What is the difference between RISC and CISC processors ? Difference between loosely coupled and tightly coupled systems ? What is an open system?
What is object in c++ example?
What is time h in c++?