//sorted arr
public class HIndex2 {
public static void main(String[] args) {
int[] arr = {0,1,3,5,6};
int start = 0;
int end = arr.length-1;
int n = arr.length;
if(n==0){
System.out.println(0);
return;
}
if(n==1){
if(arr[0]==0){
System.out.println(0);
}else{
System.out.println(1);
}
return;
}
while (start<=end){
int mid = start+(end-start)/2;
if(arr[mid]==n-mid){
System.out.println(n-mid);
return;
}else if(arr[mid]>n-mid){
end = mid-1;
}else{
start = mid+1;
}
}
System.out.println(n-start);
}
}
No comments:
Post a Comment