Given N strings which represents all integers from 0 to N in binary format except any one. The task is to find the missing number. Input consists of an array of strings where array elements are represented in binary format.

**Examples:**

Input:arr[] = {“0000”, “0001”, “0010”, “0100”}

Output:3

Input:arr[] = {“0000”, “0001”, “0010”, “0011”, “0100”, “0110”, “0111”, “1000”}

Output: 5

**Approach:**

- An imbalance of 1’s and 0’s in the least significant bits of the numbers can be observed in the N integers given. Since one number is missing either a 0 or 1 from the LSB is missing. If the number which is missing has LSB = 0 then count(1) will be greater than equal to count(0). If LSB of missing number is 1 then count(1) is less than count(0).
- From the step 1 one can easily determine the LSB of missing number.
- Once determined, discard all the numbers having LSB different from that of the missing number, i.e., if the missing number has LSB = 0, then discard all the numbers with LSB = 1 and vice versa.
- Continue the process from step 1 all over again and recur for the next LSB.
- Continue with the above process till all the bits are traversed.

Below is the impplementation of the above approach:

`// C++ program to find the missing integer ` `// in N numbers when N bits are given ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `class` `BitInteger { ` `private` `: ` ` ` `bool` `* bits; ` ` ` `public` `: ` ` ` `static` `const` `int` `INTEGER_SIZE = 32; ` ` ` ` ` `BitInteger() ` ` ` `{ ` ` ` `bits = ` `new` `bool` `[INTEGER_SIZE]; ` ` ` `} ` ` ` ` ` `// Constructor to convert an integer ` ` ` `// variable into binary format ` ` ` `BitInteger(` `int` `value) ` ` ` `{ ` ` ` `bits = ` `new` `bool` `[INTEGER_SIZE]; ` ` ` ` ` `for` `(` `int` `j = 0; j < INTEGER_SIZE; j++) { ` ` ` ` ` `// The if statement will shift the ` ` ` `// original value j times. ` ` ` `// So that appropriate (INTEGER_SIZE - 1 -j)th ` ` ` `// bits will be either 0/1. ` ` ` `// (INTEGER_SIZE - 1 -j)th bit for all ` ` ` `// j = 0 to INTEGER_SIZE-1 corresponds ` ` ` `// to LSB to MSB respectively. ` ` ` `if` `(((value >> j) & 1) == 1) ` ` ` `bits[INTEGER_SIZE - 1 - j] = ` `true` `; ` ` ` `else` ` ` `bits[INTEGER_SIZE - 1 - j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` `// Constructor to convert a ` ` ` `// string into binary format. ` ` ` `BitInteger(string str) ` ` ` `{ ` ` ` `int` `len = str.length(); ` ` ` `int` `x = INTEGER_SIZE - len; ` ` ` `bits = ` `new` `bool` `[INTEGER_SIZE]; ` ` ` ` ` `// If len = 4. Then x = 32 - 4 = 28. ` ` ` `// Hence iterate from ` ` ` `// bit 28 to bit 32 and just ` ` ` `// replicate the input string. ` ` ` `int` `i = 0; ` ` ` ` ` `for` `(` `int` `j = x; j <= INTEGER_SIZE && i < len; j++, i++) { ` ` ` `if` `(str[i] == ` `'1'` `) ` ` ` `bits[j] = ` `true` `; ` ` ` `else` ` ` `bits[j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// this function fetches the kth bit ` ` ` `int` `fetch(` `int` `k) ` ` ` `{ ` ` ` `if` `(bits[k]) ` ` ` `return` `1; ` ` ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// this function will set a value ` ` ` `// of bit indicated by k to given bitValue ` ` ` `void` `set(` `int` `k, ` `int` `bitValue) ` ` ` `{ ` ` ` `if` `(bitValue == 0) ` ` ` `bits[k] = ` `false` `; ` ` ` `else` ` ` `bits[k] = ` `true` `; ` ` ` `} ` ` ` ` ` `// convert binary representation to integer ` ` ` `int` `toInt() ` ` ` `{ ` ` ` `int` `n = 0; ` ` ` `for` `(` `int` `i = 0; i < INTEGER_SIZE; i++) { ` ` ` `n = n << 1; ` ` ` `if` `(bits[i]) ` ` ` `n = n | 1; ` ` ` `} ` ` ` `return` `n; ` ` ` `} ` `}; ` ` ` `// Function to find the missing number ` `int` `findMissingFunc(list<BitInteger>& myList, ` `int` `column) ` `{ ` ` ` `// This means that we have processed ` ` ` `// the entire 32 bit binary number. ` ` ` `if` `(column < 0) ` ` ` `return` `0; ` ` ` ` ` `list<BitInteger> oddIndices; ` ` ` `list<BitInteger> evenIndices; ` ` ` ` ` `for` `(BitInteger t : myList) { ` ` ` ` ` `// Initially column = LSB. So ` ` ` `// if LSB of the given number is 0, ` ` ` `// then the number is even and ` ` ` `// hence we add it to evenIndices list. ` ` ` `// else if LSB = 0 then add it to oddIndices list. ` ` ` `if` `(t.fetch(column) == 0) ` ` ` `evenIndices.push_back(t); ` ` ` `else` ` ` `oddIndices.push_back(t); ` ` ` `} ` ` ` ` ` `// Step 1 and Step 2 of the algorithm. ` ` ` `// Here we determine the LSB bit of missing number. ` ` ` ` ` `if` `(oddIndices.size() >= evenIndices.size()) ` ` ` ` ` `// LSB of the missing number is 0. ` ` ` `// Hence it is an even number. ` ` ` `// Step 3 and 4 of the algorithm ` ` ` `// (discarding all odd numbers) ` ` ` `return` `(findMissingFunc(evenIndices, column - 1)) << 1 | 0; ` ` ` ` ` `else` ` ` `// LSB of the missing number is 1. ` ` ` `// Hence it is an odd number. ` ` ` `// Step 3 and 4 of the algorithm ` ` ` `// (discarding all even numbers) ` ` ` `return` `(findMissingFunc(oddIndices, column - 1)) << 1 | 1; ` `} ` ` ` `// Function to return the missing integer ` `int` `findMissing(list<BitInteger>& myList) ` `{ ` ` ` `// Initial call is with given array and LSB. ` ` ` `return` `findMissingFunc(myList, BitInteger::INTEGER_SIZE - 1); ` `} ` ` ` `// Driver Code. ` `int` `main() ` `{ ` ` ` ` ` `// a corresponds to the input array which ` ` ` `// is a list of binary numbers ` ` ` `list<BitInteger> a = { BitInteger(` `"0000"` `), BitInteger(` `"0001"` `), ` ` ` `BitInteger(` `"0010"` `), BitInteger(` `"0100"` `), ` ` ` `BitInteger(` `"0101"` `) }; ` ` ` `int` `missing1 = findMissing(a); ` ` ` `cout << missing1 << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** O(N)

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.

## Recommended Posts:

- Find if a given string can be represented from a substring by iterating the substring ānā times
- Find winner of an election where votes are represented as candidate names
- Find the missing number in a string of numbers with no separator
- Find the missing value from the given equation a + b = c
- Find the last two missing digits of the given phone number
- Find the missing digit x from the given expression
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Multiply Large Numbers represented as Strings
- Divide large number represented as string
- Evaluate a boolean expression represented as string
- Add two numbers represented by two arrays
- Product of nodes at k-th level in a tree represented as string
- Square of large number represented as String
- Product of nodes at k-th level in a tree represented as string using Recursion
- Reverse all the word in a String represented as a Linked List
- Missing characters to make a string Pangram
- Missing Permutations in a list
- Make lexicographically smallest palindrome by substituting missing characters
- Perform n steps to convert every digit of a number in the format [count][digit]
- C program to print the length of a String using %n format specifier

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.