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.
Re: 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.
Re: 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.
Re: 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.
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
Re: 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.
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)
Re: 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.
Re: 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.
Re: 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.
I have tried it with c#. It is of O(2n)~O(n)
public bool tellIfDuplicate(params int[] nums)
{
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] != i + 1)
{
int temp = nums[i];
nums[i] = nums[nums[i] - 1];
nums[nums[i] - 1] = temp;
}
}
for(int i=0;i<nums.Length;i++)
if(nums[i]!=i+1)
return true;
return false;
}
Re: 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.
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;
}
}
Re: 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.
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.
Re: 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.
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...
Re: 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.
Re: 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.
@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.
Re: 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.
just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.)
//array is a[]
//len=length of array
for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+7;
}
//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/7)<<endl;
}
Re: 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.
srry.....few mistakes in above soln.....
just add as many n(length of array) to an index as much it
has duplicates i.e. if array has size 7 and no. 5 is written
3 times then add 7 three times.so that after first loop
completion, you can just take division (a[5-1]/7 in this
case.so u got 3.also original data at a[5-1] cant be loss
bcoz u can just tak (a[5-1]%len) nd u get the original data)
//array is a[]
//len=length of array
for(i=0;i<len;i++)
{
b=a[i]%len;
a[b-1]=a[b-1]+len;
}
//printing output
for(i=0;i<len;i++)
{
cout<<i+1<<" occurs: "<<(a[i]/len)<<endl;
}
Re: 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.
If there are no duplicates, Sum of numbers should be equal
to (N(N+1))/2.
Otherwise, duplicate is present.
No duplicate:
------------
Say N = 5
{1,2,3,4,5}
5(6)/2 = 15
1+2+3+4+5 = 15
Therefore no duplicate.
Duplicate:
---------
{1,2,3,4,1}
Sum = 11
which is not equal to 15 (N(N+1)/2)
Therefore duplicate present.
main()
{
int i, j, *p;
i = 25;
j = 100;
p = &i; // Address of i is assigned to pointer p
printf("%f", i/(*p) ); // i is divided by pointer p
}
a. Runtime error.
b. 1.00000
c. Compile error
d. 0.00000
Write out a function that prints out all the permutations of
a string.
For example, abc would give you abc, acb, bac, bca, cab,
cba. You can assume that all the characters will be unique.