"Coding is just like talking to a unknown girl. Once you talked to her with guts, the next time you'll never afraid",Really? Yes , try it Now !!!
Everything here is for education and learning purpose only , individual holds rights on outer posts/links etc.
Team Cracking The Code
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 :
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;
}
@hemant yes it can be done in logn thanks for code :)
Shashank
Post a Comment