Minimum count of indices to be skipped for every index of Array to keep sum till that index at most T | Function to calculate minimum indices to be skipped so that sum till i remains smaller than T ; Store the sum of all indices before i ; Store the elements that can be skipped ; Traverse the array , A [ ] ; Store the total sum of elements that needs to be skipped ; Store the number of elements need to be removed ; Traverse from the back of map so as to take bigger elements first ; Update sum ; Update map with the current element ; Given Input ; Function Call
function skipIndices ( N , T , arr ) { let sum = 0 ; let count = new Map ( ) ; for ( let i = 0 ; i < N ; i ++ ) { let d = sum + arr [ i ] - T ; let k = 0 ; if ( d > 0 ) { for ( let u of [ ... count . keys ( ) ] . reverse ( ) ) { let j = u ; let x = j * count . get ( j ) ; if ( d <= x ) { k += Math . floor ( ( d + j - 1 ) / j ) ; break ; } k += count . get ( j ) ; d -= x ; } } sum += arr [ i ] ; if ( count . has ( arr [ i ] ) ) { count . set ( arr [ i ] , count . get ( i ) + 1 ) } else { count . set ( arr [ i ] , 1 ) } document . write ( k + " " ) ; } } let N = 7 ; let T = 15 ; let arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; skipIndices ( N , T , arr ) ;