Check if it ’ s possible to completely fill every container with same ball | A boolean function that returns true if it 's possible to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Given Input ; Function Call
function getans ( i , v , q ) { if ( i == q . length ) return true ; for ( let j = 0 ; j < v . length ; j ++ ) { if ( v [ j ] >= q [ i ] ) { v [ j ] -= q [ i ] ; if ( getans ( i + 1 , v , q ) ) { return true ; } v [ j ] += q [ i ] ; } } return false ; } function Check ( c , b ) { let m = new Map ( ) ; for ( let i = 0 ; i < b . length ; i ++ ) { if ( m . has ( b [ i ] ) ) { m . set ( b [ i ] , m . get ( b [ i ] ) + 1 ) ; } else { m . set ( b [ i ] , 1 ) ; } } let v = [ ] ; for ( i of m ) { v . push ( i [ 1 ] ) ; } let check = getans ( 0 , v , c ) ; if ( check ) document . write ( " " ) ; else document . write ( " " ) ; } let c = [ 1 , 3 , 3 ] ; let b = [ 2 , 2 , 2 , 2 , 4 , 4 , 4 ] ; Check ( c , b ) ;