# Construct a K-length binary string from an array based on given conditions

Given an array **arr[] **consisting of **N **integers, and an integer **K**, the task is to construct a binary string of length **K** satisfying the following conditions:

- The character at
**i**index is ‘^{th}**1′**if a subset with sum**i**can be formed from the array. - Otherwise, the character at
**i**index is^{th}**‘0’**.

**Examples:**

Input:arr[] = {1, 4}, K = 5Output:10011Explanation:

Character at 1st index can be made by ‘1’ considering the subset {1}.

Character at 4th index can be made by ‘1’ considering the subset {4}.

Character at 5th index can be made by ‘1’ considering the subset {1, 4}.

Input:arr[] = {1, 6, 1}, K = 8Output:11000111

**Approach: **The idea is to use a greedy approach to solve this problem. Below are the steps:

- Initialize a bitset, say
**bit[]**, of size**10**and set^{5 }+ 5**bit[0] = 1.** - Traverse through the array and for each array element
**arr[i]**, update**bit**as**bit |= bit << arr[i]**to have**bit****p**if**p**can be obtained as a**subset sum.** - At
**i**iteration,^{th}**bit[i]**stores the initial sum and after performing**bit****<< arr[i]**, all bits are shifted by**arr[i]**. Therefore, bit**p**becomes**p + arr[i].** - Finally,
**bit****| (bit << arr[i])**merges these two cases, whether to consider the**i**position or not.^{th} - Iterate from
**1**to**K**and print every value**bit[i]**as the required binary string.

Below is the implementation of the above approach:

## C++

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

## Python3

`# Python program for the above approach` `# To construct the` `# required binary string` `#bit = [0]*100003` `# Function to construct binary string` `# according to the given conditions` `def` `constructBinaryString(arr,N, K):` ` ` ` ` `# Initialize with 1` ` ` `bit ` `=` `1` ` ` ` ` `# Traverse the array` ` ` `for` `i ` `in` `range` `(` `0` `, N):` ` ` ` ` `# To check if the i-th eger` ` ` `# needs to be considered or not` ` ` `bit |` `=` `bit << arr[i]` ` ` ` ` `# Print the binary string` ` ` `#for i in range(1,K):` ` ` `# print(bit[i])` ` ` `bit ` `=` `bin` `(bit).replace(` `"0b"` `, "")` ` ` `print` `(bit[` `1` `:K ` `+` `1` `])` ` ` `# Driver Code` `# Given array` `arr ` `=` `[` `1` `, ` `6` `, ` `1` `]` `# Size of the array` `N ` `=` `len` `(arr)` `# Given K` `K ` `=` `8` `constructBinaryString(arr, N, K)` `# This code is contributed by shubhamsingh10` |

**Output:**

11000111

**Time Complexity:** O(N)**Auxiliary Space:** O(N)