kumarasvn
BAN USERpublic static int search(int a[], int l, int u, int x) {
while (l <= u) {
int m = (l + u) / 2;
if (x == a[m]) {
return m;
} else if (a[l] <= a[m]) {
if (x > a[m]) {
l = m+1;
} else if (x >=a [l]) {
u = m-1;
} else {
l = m+1;
}
}
else if (x < a[m]) u = m-1;
else if (x <= a[u]) l = m+1;
else u = m - 1;
}
return -1;
}
This is working perfectly guys....
import java.util.Arrays;
public class FindNeartoZero {
public static void main(String[] args) {
int a[]={-10,-20,-30,-10,-50,-1,1};
Arrays.sort(a);
int i;
for(i=0;i<a.length;i++)
{
if(a[i]>0)
{
if(i==0)
{
System.out.println(a[i]);
System.exit(0);
}
int k=a[i-1];
System.out.println(Math.abs(k)<Math.abs(a[i])?k:a[i]);
System.exit(0);
}
}
if(i==a.length)
System.out.println(a[--i]);
}
}
Hi Sparky,
Even i lost there but i find out another solution for finding where the loop is starting.
1)First find the meeting point using slow and fast pointers
2)Find out the number of elements present in the loop(say n nodes)
3)Find out the nth node from last(Starting from meeting point) that nth node become the start of the loop.
superrrrrr... thanks dude...
- kumarasvn June 20, 2011