Sudo Placement | Placement Tour | This function returns true if the value cumulative according to received integer K is less than budget B , otherwise returns false ; Initialize a temporary array which stores the cumulative value of the original array ; Sort the array to find the smallest K values ; Check if the value is less than budget ; This function prints the optimal number of elements and respective cumulative value which is less than the given number ; Initialize answer as zero as optimal value may not exists ; If the current Mid Value is an optimal value , then try to maximize it ; Call Again to set the corresponding cumulative value for the optimal ans ; Driver Code ; Budget
let cumulativeValue = 0 ; function canBeOptimalValue ( K , arr , N , B ) { let tmp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) tmp [ i ] = ( arr [ i ] + K * ( i + 1 ) ) ; tmp . sort ( ( a , b ) => a - b ) ; cumulativeValue = 0 ; for ( let i = 0 ; i < K ; i ++ ) cumulativeValue += tmp [ i ] ; return cumulativeValue <= B ; } function findNoOfElementsandValue ( arr , N , B ) { let ans = 0 ; while ( start <= end ) { let mid = Math . floor ( ( start + end ) / 2 ) ; if ( canBeOptimalValue ( mid , arr , N , B ) ) { ans = mid ; start = mid + 1 ; } else end = mid - 1 ; } canBeOptimalValue ( ans , arr , N , B , cumulativeValue ) ; document . write ( ans + " " + cumulativeValue + " " ) ; } let arr = [ 1 , 2 , 5 , 6 , 3 ] ; let N = arr . length ; let B = 90 ; findNoOfElementsandValue ( arr , N , B ) ;