First strictly greater element in a sorted array in Java | Javascript program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code
function next ( arr , target ) { let start = 0 , end = arr . length - 1 ; let ans = - 1 ; while ( start <= end ) { let mid = parseInt ( ( start + end ) / 2 , 10 ) ; if ( arr [ mid ] <= target ) { start = mid + 1 ; } else { ans = mid ; end = mid - 1 ; } } return ans ; } let arr = [ 1 , 2 , 3 , 5 , 8 , 12 ] ; document . write ( next ( arr , 8 ) ) ;