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 / zhixingren

The following algorithm works. It is O(n) and no extra
storage required
public static bool HaveDuplicate(int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
int tmp = nums[i];
if (tmp < 0)
tmp = -tmp;

if (nums[tmp - 1] < 0)
{
return true;
}
else
{
nums[tmp - 1] = -nums[tmp - 1];
}
}
return false;
}
}

Is This Answer Correct ?    55 Yes 13 No

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

Answer / ashutosh k

You can use intermediate step for count sort.

Take a new array B of size N, initialize all values to 0.
and then
for each i from 1 to N,
if((++B[A[i]]) > 1) - there are duplicates.

--- Time complexity O(n)

Is This Answer Correct ?    29 Yes 7 No

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

Answer / monika

the formula given for summation shud be (n*(n+1))/2

but it will solve the problem only if we have one duplicate.
if we have many duplicates then we can still get the
correct summation
for ex:
1 2 2 5 5

it has two duplicates but still total is 5*(5+1)/2=15

Is This Answer Correct ?    28 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 / shri

#include<stdio.h>
void main()
{
int a[3],j,i,n;
clrscr();
printf("enter n");
scanf("%d",&n);
printf("enter els");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
printf("elements %d and %d are same",i+1,j+1);
}
}
}

Is This Answer Correct ?    23 Yes 9 No

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

Answer / raghuram

guys..sort the array using quicksort..and check adjacent
elements..u can know there are duplicates or
not..complexity-O(nlogn)+n-1

Is This Answer Correct ?    22 Yes 10 No

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

Answer / miraj

When its given you are allowed to destroy the array..do
think in that direction..


Read the first element go to that position, read that
position and mark it zero.. keep doing so...

If at any place we find zero.. that means array contains
duplicate.

Is This Answer Correct ?    19 Yes 14 No

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

Answer / anyone

If you mark places with zero, you will lose the original
value in that place which, eventually, leads to errors...

Assume an array starting as: 1-5-5-...

After first step array would be: 1-0-5-...

You will get stuck at the second step since every element
that you marked with zero will lead to the position 0.

Converting to negative makes more sense...

Is This Answer Correct ?    14 Yes 9 No

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

Answer / michael

hey scrubs, use a hash map

loop through array {
if(hash[x] == x] return true// is dupe
else hash.add(x,x);
}

max O(n)

Is This Answer Correct ?    6 Yes 4 No

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

Answer / somebody

@Michael

Moron you are using extra space there.that is obvious.using
extra space many tough problems can be solved.without extra
space tell some better methods.

Is This Answer Correct ?    5 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 / ronstar luan

We need not sort this array and we need save space.
{2, 4, 5, 1, 3} m=2;
{2, 2, 5, 1, 3} m=4;
{2, 2, 5, 4, 3} m=1;
{1, 2, 5, 4, 3} m=2;
--------------------
{1, 2, 5, 4, 3} m=5;
{1, 2, 5, 4, 5} m=3;
{1, 2, 3, 4, 5} m=5;
--------------------

Is This Answer Correct ?    4 Yes 3 No

Post New Answer

More C Code Interview Questions

what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  


main() { void swap(); int x=10,y=8; swap(&x,&y); printf("x=%d y=%d",x,y); } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; }

2 Answers  


enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }

2 Answers  


main() { char *p = “ayqm”; printf(“%c”,++*(p++)); }

29 Answers   IBM, TCS, UGC NET, Wipro,


void main() { int i; char a[]="\0"; if(printf("%s\n",a)) printf("Ok here \n"); else printf("Forget it\n"); }

3 Answers   Accenture,






#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


What is data _null_? ,Explain with code when u need to use it in data step programming ?

0 Answers   Abbott,


main() { extern int i; i=20; printf("%d",i); }

1 Answers   Value Labs,


What is full form of PEPSI

0 Answers  


What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


Categories