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 |
#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }
plz send me all data structure related programs
Write a procedure to implement highlight as a blinking operation
main() { unsigned int i=10; while(i-->=0) printf("%u ",i); }
how many processes will gate created execution of -------- fork(); fork(); fork(); -------- Please Explain... Thanks in advance..!
how to test pierrot divisor
print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!!
35 Answers Tata Elxsi, TCS, VI eTrans,
1. const char *a; 2. char* const a; 3. char const *a; -Differentiate the above declarations.
How we print the table of 2 using for loop in c programing?
what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }
#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }
Who could write how to find a prime number in dynamic array?