Skip to content
Related Articles

Related Articles

Construct a K-length binary string from an array based on given conditions
  • Last Updated : 03 Mar, 2021

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: 
 

  1. The character at ith index is ‘1′ if a subset with sum i can be formed from the array.
  2. Otherwise, the character at ith index is ‘0’.

Examples:

Input: arr[] = {1, 4}, K = 5
Output: 10011
Explanation: 
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 = 8
Output: 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 105 + 5 and set 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 ith iteration, 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 ith position or not.
  • 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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :