I have a sorted array of numbers with array length as n
. For a given item k
find index i
in the sorted array where elements from index i
to n
are greater than the given item k
. If there is no index present then return -1
This is my program:
public static int getIndex(int[] arr, int k) {
int x = -1;
for(int i=0; i<arr.length; i++) {
if(arr[i] > k) {
x = i; break;
}
}
return x;
}
The time complexity of this approach is O(n), and I want to reduce it further.