Find Binary permutations of given size not present in the Array
Given a positive integer N and an array arr[] of size K consisting of binary string where each string is of size N, the task is to find all the binary strings of size N that are not present in the array arr[].
Examples:
Input: N = 3, arr[] = {“101”, “111”, “001”, “010”, “011”, “100”, “110”}
Output: 000
Explanation: Only 000 is absent in the given array.
Input: N = 2, arr[] = {“00”, “01”}
Output: 10 11
Approach: The given problem can be solved by finding all binary strings of size N using Backtracking and then print those strings that are not present in the array arr[]. Follow the steps below to solve the problem:
- Initialize an unordered_set of strings S that stores all the strings in the array arr[].
- Initialize a variable, say total and set it as the power of 2N where N is the size of the string.
- Iterate over the range [0, total) using the variable i and perform the following steps:
- Initialize an empty string variable num as “”.
- Iterate over the range [N – 1, 0] using the variable j and if the value of the Bitwise AND of i and 2j is true, then push the character 1 into the string num. Otherwise, push the character 0.
- If num is not present in the set S then print the string num as the one of the resultant string.
- After completing the above steps, if all the possible binary strings are present in the array then print “-1”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMissingBinaryString(vector<string>& nums, int N)
{
unordered_set<string> s;
int counter = 0;
for (string str : nums) {
s.insert(str);
}
int total = ( int ) pow (2, N);
string ans = "" ;
for ( int i = 0; i < total; i++) {
string num = "" ;
for ( int j = N - 1; j >= 0; j--) {
if (i & (1 << j)) {
num += '1' ;
}
else {
num += '0' ;
}
}
if (s.find(num) != s.end()) {
continue ;
counter++;
}
else {
cout << num << ", " ;
}
}
if (counter == total) {
cout << "-1" ;
}
}
int main()
{
int N = 3;
vector<string> arr
= { "101" , "111" , "001" , "011" , "100" , "110" };
findMissingBinaryString(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMissingBinaryString(String[] nums, int N)
{
HashSet<String> s = new HashSet<String>();
int counter = 0 ;
for (String str : nums) {
s.add(str);
}
int total = ( int )Math.pow( 2 , N);
String ans = "" ;
for ( int i = 0 ; i < total; i++) {
String num = "" ;
for ( int j = N - 1 ; j >= 0 ; j--) {
if ((i & ( 1 << j))> 0 ) {
num += '1' ;
}
else {
num += '0' ;
}
}
if (s.contains(num)) {
counter++;
continue ;
}
else {
System.out.print(num+ ", " );
}
}
if (counter == total) {
System.out.print( "-1" );
}
}
public static void main(String[] args)
{
int N = 3 ;
String []arr
= { "101" , "111" , "001" , "011" , "100" , "110" };
findMissingBinaryString(arr, N);
}
}
|
Python3
from math import pow
def findMissingBinaryString(nums, N):
s = set ()
counter = 0
for x in nums:
s.add(x)
total = int ( pow ( 2 , N))
ans = ""
for i in range (total):
num = ""
j = N - 1
while (j > = 0 ):
if (i & ( 1 << j)):
num + = '1'
else :
num + = '0'
j - = 1
if (num in s):
continue
counter + = 1
else :
print (num,end = ", " )
if (counter = = total):
print ( "-1" )
if __name__ = = '__main__' :
N = 3
arr = [ "101" , "111" , "001" , "011" , "100" , "110" ]
findMissingBinaryString(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findMissingBinaryString( string [] nums,
int N)
{
HashSet< string > s = new HashSet< string >();
int counter = 0;
foreach ( string str in nums) { s.Add(str); }
int total = ( int )Math.Pow(2, N);
for ( int i = 0; i < total; i++) {
string num = "" ;
for ( int j = N - 1; j >= 0; j--) {
if ((i & (1 << j)) > 0) {
num += '1' ;
}
else {
num += '0' ;
}
}
if (s.Contains(num)) {
counter++;
continue ;
}
else {
Console.Write(num + ", " );
}
}
if (counter == total) {
Console.Write( "-1" );
}
}
public static void Main( string [] args)
{
int N = 3;
string [] arr
= { "101" , "111" , "001" , "011" , "100" , "110" };
findMissingBinaryString(arr, N);
}
}
|
Javascript
<script>
function findMissingBinaryString(nums, N) {
let s = new Set();
let counter = 0;
for (let str of nums) {
s.add(str);
}
let total = Math.pow(2, N);
let ans = "" ;
for (let i = 0; i < total; i++) {
let num = "" ;
for (let j = N - 1; j >= 0; j--) {
if (i & (1 << j)) {
num += "1" ;
} else {
num += "0" ;
}
}
if (s.has(num)) {
continue ;
counter++;
}
else {
document.write(num + ", " );
}
}
if (counter == total) {
document.write( "-1" );
}
}
let N = 3;
let arr = [ "101" , "111" , "001" , "011" , "100" , "110" ];
findMissingBinaryString(arr, N);
</script>
|
Time Complexity: O(N*2N)
Auxiliary Space: O(K), where K is the size of the array
Last Updated :
20 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...