Count the combination of 4 s and / or 5 s required to make each Array element 0 | Java program for the above approach ; 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 ; Driver Code ; Given Input ; Function Call
import java . io . * ; import java . util . Arrays ; class GFG { static void sumOfCombinationOf4OR5 ( int [ ] arr , int N ) { int [ ] ans = new int [ N ] ; Arrays . fill ( ans , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 4 ) { continue ; } int sum = Integer . MAX_VALUE ; int cnt = 0 ; for ( int j = 0 ; j <= arr [ i ] ; j += 4 ) { if ( ( arr [ i ] - j ) % 5 == 0 ) { sum = Math . min ( sum , cnt + ( arr [ i ] - j ) / 5 ) ; } cnt ++ ; } if ( sum != Integer . MAX_VALUE ) ans [ i ] = sum ; } for ( int num : ans ) System . out . printf ( num + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 15 , 17 , 22 } ; int N = arr . length ; sumOfCombinationOf4OR5 ( arr , N ) ; } }