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

main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }

1 Answers  


main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

1 Answers  


What is "far" and "near" pointers in "c"...?

3 Answers  


write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

0 Answers   Nagarro,


how to return a multiple value from a function?

5 Answers   Wipro,






#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }

1 Answers  


main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); }

2 Answers  


main() { int i; float *pf; pf = (float *)&i; *pf = 100.00; printf("\n %d", i); } a. Runtime error. b. 100 c. Some Integer not 100 d. None of the above

2 Answers   HCL, LG,


Write a program using one dimensional array to assign values and then display it on the screen. Use the formula a[i]=i*10 to assign value to an element.

1 Answers   Samar State University,


Finding a number which was log of base 2

1 Answers   NetApp,


&#8206;#define good bad main() { int good=1; int bad=0; printf ("good is:%d",good); }

2 Answers  


why nlogn is the lower limit of any sort algorithm?

0 Answers  


Categories