Binary search first >= target
Java
Hard
5 views
Problem Description
Task: return the first index where a[i] >= target in a sorted array, else return n.
Output Format
Return value
Constraints
Use classic binary search.
Official Solution
static int lowerBound(int[] a,int target){int l=0,r=a.length;while(l<r){int m=l+(r-l)/2; if(a[m]>=target) r=m; else l=m+1;}return l;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!