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.

Answers were Sorted based on User's Feedback



Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / sankalan

sum all those no. say you get x
say there is a b (repeated) in place of a
so a-b = n(n+1)/2 - x=m(say)
sum the squares of those nos
say you get y
a^2-b^2 = 1/6(n+1)(2n+1)-y=n(say)
(a^-b^2)/(a-b)=a+b=m/n
to get a ((a+b)+(a-b))/2
to get b ((a+b)-(a-b))/2

Is This Answer Correct ?    1 Yes 0 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / ashish

srry.....few mistakes in above soln.....


just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.also original data at a[5-1] cant be loss
bcoz u can just tak (a[5-1]%len) nd u get the original data)


//array is a[]
//len=length of array


for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+len;
}
//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/len)<<endl;
}

Is This Answer Correct ?    1 Yes 1 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / faykarta

I'm nearly 100% sure that the only way to do this without
allocating more space is to loop through the entire array
checking elements against each other. Because its an
iterative method it can be optimised for linear memory access.

consider:

for(i = 0; i < N; ++i)
{
for(j = i + 1; j < N; ++j)
{
if(array[i] == array[j])
{
//index look up avoided through compiler optimisation???
return true;
}
}
}
return false;

*(Cache Integrity)The optimisation could be made using
pointer arithmetic or iterator style traversal in C/C++ anyway.

You could perform a custom sort operation that throws out
but i think it will be about the same algorithmic complexity
with less linear memory access and more complex code....

consider: (havent tested this one)

for(i = 0; i < N; ++i)
{
j = array[i] - 1;
while(j != i)
{
//check
if(array[i] == array[j])
{
return true;
}
//swap
array[i] ^= array[j] ^= array[i] ^= array[j];
j = array[i] - 1;
}
}
return false;

Any thoughts on these?
I would stick with brute force method.

Is This Answer Correct ?    1 Yes 1 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / foobar

You can use a hashtable, populate the keys from 1 to n.

while you re reading, mark the number you read and increment
their values by 1. if if you see the same number again,
fail. that s a dup. runs in O(n).

or you can use bucket sort.

Is This Answer Correct ?    0 Yes 0 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / hitesh

#include<iostream>
using namespace std;
int main()
{
int a[5],j,temp,flag,i,s ;
cout<<"Enter the size of the array :";
cin>>s; // size

for( i=0;i<s;i++){
cout<<"Enter the "<<i+1<<" element :";
cin>>a[i];
}

for( int i=0;i<s;i++){
temp=a[i];
for(j=i+1;j<s;j++){

if(a[j]==temp)
{flag=1;
break;
}
}

}
if(flag==1){cout<<"Duplicacy is there !! The duplicate number is :"<<temp;
}
else
cout<<"No duplicate value";
return 0;

}

Is This Answer Correct ?    0 Yes 0 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / madhu h gopala

@arr = (6,7,4,1,9,8,7,1);
print scalar @arr;
%seen = map {$_ => 1} @arr;
print scalar keys %seen

If the size of the array varies then there are duplicates

Is This Answer Correct ?    0 Yes 0 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / ashish

just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.)


//array is a[]
//len=length of array


for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+7;
}
//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/7)<<endl;
}

Is This Answer Correct ?    0 Yes 1 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / someone

Answer # eight by Zhixingren has one issue
consider following case
[3,3,1,4,5]

Here after processing first number value at index 2 will be
negative and after processing second number it will become
positive again

so when we process index it will have positive value and
code will not detect duplicate.

Mark no as negative if it is not negative will solve the
problem.

Is This Answer Correct ?    1 Yes 3 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / yegullelew

I have tried it with c#. It is of O(2n)~O(n)
public bool tellIfDuplicate(params int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] != i + 1)
{
int temp = nums[i];
nums[i] = nums[nums[i] - 1];
nums[nums[i] - 1] = temp;
}
}
for(int i=0;i<nums.Length;i++)
if(nums[i]!=i+1)
return true;
return false;
}

Is This Answer Correct ?    8 Yes 12 No

Given an array of size N in which every number is between 1 and N, determine if there are any dupli..

Answer / murugesh

If there are no duplicates, Sum of numbers should be equal
to (N(N+1))/2.

Otherwise, duplicate is present.

No duplicate:
------------

Say N = 5

{1,2,3,4,5}

5(6)/2 = 15

1+2+3+4+5 = 15

Therefore no duplicate.

Duplicate:
---------

{1,2,3,4,1}

Sum = 11

which is not equal to 15 (N(N+1)/2)

Therefore duplicate present.

Is This Answer Correct ?    1 Yes 15 No

Post New Answer

More C Code Interview Questions

What is the subtle error in the following code segment? void fun(int n, int arr[]) { int *p=0; int i=0; while(i++<n) p = &arr[i]; *p = 0; }

1 Answers  


main() { printf("%d, %d", sizeof('c'), sizeof(100)); } a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

18 Answers   HCL, IBM, Infosys, LG Soft, Satyam,


#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }

1 Answers  


Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

3 Answers  






plz send me all data structure related programs

2 Answers  


#include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); }

1 Answers   TCS,


main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }

1 Answers  


main() { int i, j; scanf("%d %d"+scanf("%d %d", &i, &j)); printf("%d %d", i, j); } a. Runtime error. b. 0, 0 c. Compile error d. the first two values entered by the user

2 Answers   HCL,


#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }

1 Answers  


Is the following code legal? typedef struct a { int x; aType *b; }aType

1 Answers  


1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.

8 Answers   IBPS, Infosys, TCS,


Categories