# Find any permutation of Binary String of given size not present in Array

Last Updated : 18 Jan, 2022

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: 00
Explanation: String “00” does not appear in array arr[]. Another valid string can be “11” which does not occur in the array arr[] as well.

Input: arr[] {“111”, “011”, “001”}
Output: 101
Explanation: String “101” does not appear in array arr[]. Another valid strings are “000”, “010”, “100”, and “110”.

Naive Approach: The given problem can be solved by generating all the binary strings having N bits and returning the 1st string such that it does not occur in the given array arr[].

Time Complexity: O(2N * N2)
Auxiliary Space: O(N)

Efficient Approach: The given problem can be optimized by using a method similar to Cantor’s Diagonal Argument. It can be observed that the resulting string must have at least one bit that is different from all the strings in arr[]. Therefore, the resulting binary string can be constructed by taking the complement of the 1st element of the 1st string as the 1st element, the complement of the 2nd element of the 2nd string as the 2nd element, and so on. Below are the steps to follow:

• Create a string ans, which stores the resulting string. Initially, it is empty.
• Iterate through the given strings in a diagonal order, i.e, 1st element of 1st string, 2nd element of 2nd string, and so on, and append the compliment of the value at the current index into the string ans.
• The string stored in ans after iterating through the complete array arr[] is one of the valid strings required.

Below is the implementation of the above approach:

## C++

 `// C++ Program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to find a binary string of` `// N bits that does not occur in the` `// given array arr[]` `string findString(vector& arr, ``int` `N)` `{` `    ``// Stores the resultant string` `    ``string ans = ``""``;`   `    ``// Loop to iterate over all the given` `    ``// strings in a diagonal order` `    ``for` `(``int` `i = 0; i < N; i++) {`   `        ``// Append the complement of element` `        ``// at current index into ans` `        ``ans += arr[i][i] == ``'0'` `? ``'1'` `: ``'0'``;` `    ``}`   `    ``// Return Answer` `    ``return` `ans;` `}`   `// Driver code` `int` `main()` `{` `    ``vector arr{ ``"111"``, ``"011"``, ``"001"` `};` `    ``int` `N = arr.size();`   `    ``cout << findString(arr, N);`   `    ``return` `0;` `}`

## Java

 `// Java Program for the above approach`   `import` `java.io.*;`   `class` `GFG {` `  `  `      ``// Function to find a binary string of` `    ``// N bits that does not occur in the` `    ``// given array arr[]` `    ``static` `String findString(String arr[], ``int` `N)` `    ``{` `        ``// Stores the resultant string` `        ``String ans = ``""``;`   `        ``// Loop to iterate over all the given` `        ``// strings in a diagonal order` `        ``for` `(``int` `i = ``0``; i < N; i++) {`   `            ``// Append the complement of element` `            ``// at current index into ans` `            ``ans += arr[i].charAt(i) == ``'0'` `? ``'1'` `: ``'0'``;` `        ``}`   `        ``// Return Answer` `        ``return` `ans;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main (String[] args) {` `           ``String arr[] = { ``"111"``, ``"011"``, ``"001"` `};` `        ``int` `N = arr.length;`   `        ``System.out.println(findString(arr, N));` `    ``}` `}`   `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python 3 Program for the above approach`   `# Function to find a binary string of` `# N bits that does not occur in the` `# given array arr[]` `def` `findString(arr, N):` `  `  `    ``# Stores the resultant string` `    ``ans ``=` `""`   `    ``# Loop to iterate over all the given` `    ``# strings in a diagonal order` `    ``for` `i ``in` `range``(N):` `      `  `        ``# Append the complement of element` `        ``# at current index into ans` `        ``ans ``+``=` `'1'` `if` `arr[i][i] ``=``=` `'0'` `else` `'0'`   `    ``# Return Answer` `    ``return` `ans`   `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``arr ``=` `[``"111"``, ``"011"``, ``"001"``]` `    ``N ``=` `len``(arr)`   `    ``print``(findString(arr, N))`   `    ``# This code is contributed by bgangwar59.`

## C#

 `// C# Program for the above approach` `using` `System;`   `class` `GFG {`   `    ``// Function to find a binary string of` `    ``// N bits that does not occur in the` `    ``// given array arr[]` `    ``static` `string` `findString(``string``[] arr, ``int` `N)` `    ``{` `      `  `        ``// Stores the resultant string` `        ``string` `ans = ``""``;`   `        ``// Loop to iterate over all the given` `        ``// strings in a diagonal order` `        ``for` `(``int` `i = 0; i < N; i++) {`   `            ``// Append the complement of element` `            ``// at current index into ans` `            ``ans += arr[i][i] == ``'0'` `? ``'1'` `: ``'0'``;` `        ``}`   `        ``// Return Answer` `        ``return` `ans;` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main(String[] args)` `    ``{` `        ``string``[] arr = { ``"111"``, ``"011"``, ``"001"` `};` `        ``int` `N = arr.Length;`   `        ``Console.WriteLine(findString(arr, N));` `    ``}` `}`   `// This code is contributed by ukasp.`

## Javascript

 ``

Output:

`000`

Time Complexity: O(N)
Auxiliary Space: O(1)

Previous
Next