Wednesday, June 28, 2006

Array[0]

There is an array of size n storing numbers between 0 to n-3 and 2 of the nums repeated. Find both of them.

Example:
I/P: 0 1 2 1 2 3
O/P: 1 2

This solution is generic for finding if there are any repeats in the array.

Solution:
for(i=0;i lessThan n; i++)
{
swap(arr[i], arr[arr[i]]);
if(i != arr[i] && arr[arr[i]] EQ arr[i])
print "found %d", arr[i]

}
Question And Answer Contributed by f2001389

4 comments:

Bart said...

I thought Indians are good in SEO (Search engine optimization), just find out that indians are also good in programming! nice blog dude ;)

Anonymous said...

The algo for finding the repeated numbers is realy cool.
It would be better if it is explained the idea behind the algo

Unknown said...

I think the behind idea is sort of the Bucket sorting.

Anonymous said...

what would happen if the array is like this

0 234 223 234 223 1 2 3 4 2 23