Minimum number of working days required to achieve each of the given scores | Function to find the lower bound of N using binary search ; Stores the lower bound ; Stores the upper bound ; Stores the minimum index having value is at least N ; Iterater while i <= j ; Stores the mid index of the range [ i , j ] ; If P [ mid ] is at least N ; Update the value of mid to index ; Update the value of j ; Update the value of i ; Return the resultant index ; Function to find the minimum number of days required to work to at least arr [ i ] points for every array element ; Traverse the array P [ ] ; Find the prefix sum ; Traverse the array arr [ ] ; Find the minimum index of the array having value at least arr [ i ] ; If the index is not - 1 ; Otherwise ; Driver Code
function binarySeach ( P , N ) { var i = 0 ; var j = P . length - 1 ; var index = - 1 ; while ( i <= j ) { var mid = i + parseInt ( ( j - i ) / 2 ) ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } function minDays ( P , arr ) { for ( var i = 1 ; i < P . length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( var i = 0 ; i < arr . length ; i ++ ) { var index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { document . write ( ( index + 1 ) + " " ) ; } else { document . write ( - 1 + " " ) ; } } } var arr = [ 400 , 200 , 700 , 900 , 1400 ] ; var P = [ 100 , 300 , 400 , 500 , 600 ] ; minDays ( P , arr ) ;