Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int minAbsNum(int[] a)
- {
- if(a[0] >= 0)
- return a[0] + a[1];
- if(a[a.length - 1] <= 0)
- return Math.abs(a[a.length - 1]) + Math.abs(a[a.length - 2]);
- int high = a.length - 1;
- int low = 0;
- int mid = 0;
- boolean found = false;
- while(low <= high && !found){
- mid = (low + high)/2;
- if(a[mid] < 0)
- low = mid + 1;
- else if(a[mid - 1] < 0)
- found = true;
- else
- high = mid - 1;
- }
- if(Math.abs(a[mid - 1]) < Math.abs(a[mid]))
- mid--;
- if(mid == 0)
- return Math.abs(a[mid]) + Math.abs(a[mid + 1]);
- if(mid == a.length - 1)
- return Math.abs(a[mid]) + Math.abs(a[mid - 1]);
- int sum1 = Math.abs(a[mid]) + Math.abs(a[mid - 1]);
- int sum2 = Math.abs(a[mid]) + Math.abs(a[mid + 1]);
- return Math.min(sum1, sum2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement