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() { char *p; printf("%d %d ",sizeof(*p),sizeof(p)); }

6 Answers  


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


Write a function to find the depth of a binary tree.

13 Answers   Adobe, Amazon, EFI, Imagination Technologies,


how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns

0 Answers  


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

0 Answers  






Program to Delete an element from a doubly linked list.

4 Answers   College School Exams Tests, Infosys,


How to return multiple values from a function?

7 Answers  


Extend the sutherland-hodgman clipping algorithm to clip three-dimensional planes against a regular paralleiepiped

1 Answers   IBM,


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


to remove the repeated cahracter from the given caracter array. i.e.., if the input is SSAD output should of SAD

6 Answers   Synergy,


Write a program to implement the motion of a bouncing ball using a downward gravitational force and a ground-plane friction force. Initially the ball is to be projected in to space with a given velocity vector

2 Answers  


main() { printf("%d", out); } int out=100;

3 Answers  


Categories