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++
// 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 arrayvoid 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 Codeint main(){ int N = 3; vector<string> arr = { "101", "111", "001", "011", "100", "110" }; findMissingBinaryString(arr, N); return 0;} |
000, 010,
Time Complexity: O(N*2N)
Auxiliary Space: O(K), where K is the size of the array
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


