Construct a K | C ++ program for the above approach ; To construct the required binary string ; Function to construct binary string according to the given conditions ; Initialize with 1 ; Traverse the array ; To check if the i - th integer needs to be considered or not ; Print the binary string ; Driver Code ; Given array ; Size of the array ; Given K
#include <bits/stdc++.h> NEW_LINE using namespace std ; bitset < 100003 > bit ; void constructBinaryString ( int arr [ ] , int N , int K ) { bit [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { bit |= bit << arr [ i ] ; } for ( int i = 1 ; i <= K ; i ++ ) { cout << bit [ i ] ; } } int main ( ) { int arr [ ] = { 1 , 6 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 8 ; constructBinaryString ( arr , N , K ) ; }