Check if it ’ s possible to completely fill every container with same ball | C ++ program for above approach ; 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 ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool getans ( int i , vector < int > v , vector < int > q ) { if ( i == q . size ( ) ) return true ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v [ j ] >= q [ i ] ) { v [ j ] -= q [ i ] ; if ( getans ( i + 1 , v , q ) ) { return true ; } v [ j ] += q [ i ] ; } } return false ; } void Check ( vector < int > c , vector < int > b ) { map < int , int > m ; for ( int i = 0 ; i < b . size ( ) ; i ++ ) { m [ b [ i ] ] ++ ; } vector < int > v ; for ( auto i : m ) { v . push_back ( i . second ) ; } bool check = getans ( 0 , v , c ) ; if ( check ) cout << " YES " << endl ; else cout << " NO " << endl ; } int main ( ) { vector < int > c = { 1 , 3 , 3 } ; vector < int > b = { 2 , 2 , 2 , 2 , 4 , 4 , 4 } ; Check ( c , b ) ; return 0 ; }