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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
how to swap 3 nos without using temporary variable
pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"
main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }
#define a 10 int main() { printf("%d..",a); foo(); printf("%d..",a); return 0; } void foo() { #undef a #define a 50 }
enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }
write a c program to Reverse a given string using string function and also without string function
void main() { char a[]="12345\0"; int i=strlen(a); printf("here in 3 %d\n",++i); }
What is full form of PEPSI
plz send me all data structure related programs
What is your nationality?
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }
Write a c program to search an element in an array using recursion