Find Binary permutations of given size not present in the Array
Last Updated :
20 Sep, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find a Binary String of
// same length other than the Strings
// present in the array
void findMissingBinaryString(vector<string>& nums, int N)
{
unordered_set<string> s;
int counter = 0;
// Map all the strings present in
// the array
for (string str : nums) {
s.insert(str);
}
int total = (int)pow(2, N);
string ans = "";
// Find all the substring that
// can be made
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 num already exists then
// increase counter
if (s.find(num) != s.end()) {
continue;
counter++;
}
// If not found print
else {
cout << num << ", ";
}
}
// If all the substrings are present
// then print -1
if (counter == total) {
cout << "-1";
}
}
// Driver Code
int main()
{
int N = 3;
vector<string> arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find a Binary String of
// same length other than the Strings
// present in the array
static void findMissingBinaryString(String[] nums, int N)
{
HashSet<String> s = new HashSet<String>();
int counter = 0;
// Map all the Strings present in
// the array
for (String str : nums) {
s.add(str);
}
int total = (int)Math.pow(2, N);
String ans = "";
// Find all the subString that
// can be made
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 num already exists then
// increase counter
if (s.contains(num)) {
counter++;
continue;
}
// If not found print
else {
System.out.print(num+ ", ");
}
}
// If all the subStrings are present
// then print -1
if (counter == total) {
System.out.print("-1");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
String []arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
}
}
// This code is contributed by Princi Singh
Python3
# Python 3 program for the above approach
from math import pow
# Function to find a Binary String of
# same length other than the Strings
# present in the array
def findMissingBinaryString(nums, N):
s = set()
counter = 0
# Map all the strings present in
# the array
for x in nums:
s.add(x)
total = int(pow(2, N))
ans = ""
# Find all the substring that
# can be made
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 already exists then
# increase counter
if (num in s):
continue
counter += 1
# If not found print
else:
print(num,end = ", ")
# If all the substrings are present
# then print -1
if (counter == total):
print("-1")
# Driver Code
if __name__ == '__main__':
N = 3
arr = ["101", "111", "001", "011", "100", "110"]
findMissingBinaryString(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find a Binary String of
// same length other than the Strings
// present in the array
static void findMissingBinaryString(string[] nums,
int N)
{
HashSet<string> s = new HashSet<string>();
int counter = 0;
// Map all the Strings present in
// the array
foreach(string str in nums) { s.Add(str); }
int total = (int)Math.Pow(2, N);
// string ans = "";
// Find all the subString that
// can be made
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 num already exists then
// increase counter
if (s.Contains(num)) {
counter++;
continue;
}
// If not found print
else {
Console.Write(num + ", ");
}
}
// If all the subStrings are present
// then print -1
if (counter == total) {
Console.Write("-1");
}
}
// Driver Code
public static void Main(string[] args)
{
int N = 3;
string[] arr
= { "101", "111", "001", "011", "100", "110" };
findMissingBinaryString(arr, N);
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// Javascript program for the above approach
// Function to find a Binary String of
// same length other than the Strings
// present in the array
function findMissingBinaryString(nums, N) {
let s = new Set();
let counter = 0;
// Map all the strings present in
// the array
for (let str of nums) {
s.add(str);
}
let total = Math.pow(2, N);
let ans = "";
// Find all the substring that
// can be made
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 num already exists then
// increase counter
if (s.has(num)) {
continue;
counter++;
}
// If not found print
else {
document.write(num + ", ");
}
}
// If all the substrings are present
// then print -1
if (counter == total) {
document.write("-1");
}
}
// Driver Code
let N = 3;
let arr = ["101", "111", "001", "011", "100", "110"];
findMissingBinaryString(arr, N);
// This code is contributed by _saurabh_jaiswal.
</script>
Time Complexity: O(N*2N)
Auxiliary Space: O(K), where K is the size of the array
Similar Reads
Find any permutation of Binary String of given size not present in Array Given an array arr[] of N distinct binary strings each having N characters, the task is to find any binary string having N characters such that it doesn't occur in the given array arr[]. Example: Input: arr[] = {"10", "01"}Output: 00Explanation: String "00" does not appear in array arr[]. Another va
5 min read
Find the Number of Permutations that satisfy the given condition in an array Given an array arr[] of size N, the task is to find the number of permutations in the array that follows the given condition: If K is the maximum element in the array, then the elements before K in the array should be in the ascending order and the elements after K in the array should be in the desc
12 min read
Print k different sorted permutations of a given array Given an array arr[] containing N integers, the task is to print k different permutations of indices such that the values at those indices form a non-decreasing sequence. Print -1 if it is not possible. Examples: Input: arr[] = {1, 3, 3, 1}, k = 3 Output: 0 3 1 2 3 0 1 2 3 0 2 1 For every permutatio
8 min read
Minimize cost to make given Array a permutation of 1 to N by given replacements Given two arrays a[] and b[] of length N and an integer K (1 ⤠K ⤠N). All the integers in array a[] lie within the range [1, K]. ith value in array b[] denotes the cost of replacing a[i] with any number in the range [1, N]. The task is to find the minimum cost to replace the elements of array a[] t
6 min read
Print all Unique permutations of a given string. Given a string that may contain duplicates, the task is find all unique permutations of given string in any order.Examples: Input: "ABC"Output: ["ABC", "ACB", "BAC", "BCA", "CAB", "CBA"]Explanation: Given string ABC has 6 unique permutations as "ABC", "ACB", "BAC", "BCA", "CAB" and "CBA".Input: "AAA
12 min read
Find permutation array from the cumulative sum array Given an array arr[] of N elements where each arr[i] is the cumulative sum of the subarray P[0...i] of another array P[] where P is the permutation of the integers from 1 to N. The task is to find the array P[], if no such P exists then print -1.Examples: Input: arr[] = {2, 3, 6} Output: 2 1 3Input:
5 min read