Count the combination of 4 s and / or 5 s required to make each Array element 0 | Function to print the count of the combination of 4 or 5 required to make the arr [ i ] for each 0 < i < N ; Vector to store the answer ; Iterate in the range [ 0 , N - 1 ] ; Initialize sum to store the count of numbers and cnt for the current factor of 4 ; Iterate in the range [ 0 , arr [ i ] ] with increment of 4 ; Check if arr [ i ] - j ( the current factor of 4 ) is divisible by 5 or not ; If sum is not maximum then answer is found ; Finally , print the required answer ; Given Input ; Function Call
function sumOfCombinationOf4OR5 ( arr , N ) { let ans = new Array ( N ) . fill ( - 1 ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } let sum = Number . MAX_SAFE_INTEGER , cnt = 0 ; for ( let j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . min ( sum , cnt + Math . floor ( ( arr [ i ] - j ) / 5 ) ) ; } cnt ++ ; } if ( sum != Number . MAX_SAFE_INTEGER ) ans [ i ] = sum ; } for ( let num of ans ) document . write ( num + " " ) ; } let arr = [ 7 , 15 , 17 , 22 ] ; let N = arr . length ; sumOfCombinationOf4OR5 ( arr , N ) ;