Monday, July 4, 2011

Given An Unsorted with lot of duplicates & having Special property with it that every concecutive pair has maximum difference of 1 & also given the 1st last element in array , WAP to Search An Element X Efficiently?

Example
1 2 1 2 3 2 3 4 5 4 6 7 6 7 8 ....10000
Given for all in i we have to search an element x(say 3) & return any index of it.


Data Structure Used: Array

Algorithm:(Done Will Post Soon)


Working Code:


Time Complexity O(logn) yes its logn ??
Space Complexity O(1)
Run Here

2 comments :

hemant said...

This is same as sorted array 1,2,3............. n and just do binary search.

mid=(start+last)/2
while(start<last)
{
if(current = arr[mid])
return mid;

if(current < arr[mid])
last=mid;
else
start=mid;
}

Unknown said...

@hemant yes it can be done in logn thanks for code :)

Shashank