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 ith index is ‘1′ if a subset with sum i can be formed from the array.
- 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++
#include <bits/stdc++.h>
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);
}
|
Java
import java.util.Arrays;
class BinaryString {
public static void main(String[] args) {
int [] arr = { 1 , 6 , 1 };
int N = arr.length;
int K = 8 ;
constructBinaryString(arr, N, K);
}
static void constructBinaryString( int [] arr, int N, int K) {
int bit = 1 ;
for ( int i = 0 ; i < N; i++) {
bit |= (bit << arr[i]);
}
String binaryString = Integer.toBinaryString(bit);
System.out.println(binaryString.substring( 1 , K));
}
}
|
Python3
def constructBinaryString(arr,N, K):
bit = 1
for i in range ( 0 , N):
bit | = bit << arr[i]
bit = bin (bit).replace( "0b" , "")
print (bit[ 1 :K + 1 ])
arr = [ 1 , 6 , 1 ]
N = len (arr)
K = 8
constructBinaryString(arr, N, K)
|
C#
using System;
using System.Linq;
namespace BinaryString {
class Program {
static void Main( string [] args)
{
int [] arr = { 1, 6, 1 };
int N = arr.Length;
int K = 8;
ConstructBinaryString(arr, N, K);
}
static void ConstructBinaryString( int [] arr, int N,
int K)
{
int bit = 1;
for ( int i = 0; i < N; i++) {
bit |= (bit << arr[i]);
}
string binaryString = Convert.ToString(bit, 2);
Console.WriteLine(binaryString.Substring(1, K));
}
}
}
|
Javascript
function constructBinaryString(arr,N, K)
{
let bit = 1
for ( var i = 0; i < N; i++)
bit |= (bit << arr[i])
bit = bit.toString(2)
console.log(bit.substring(1, K + 1))
}
let arr = [1, 6, 1]
let N = arr.length
let K = 8
constructBinaryString(arr, N, K)
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
04 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...