1. There will be no leading zeros in any string in the list ‘BINARYNUMS’.
Consider N = 5 and the list ‘binaryNums’= [“0”, “01”, “010”, “100”, “101”]. This list consists of the binary representation of numbers [0, 1, 2, 4, 5]. Clearly, the missing number is 3 and its binary representation will be “11”. So you should return string “11”.
The first line of input contains an integer ‘T’ denoting the number of test cases. then ‘T’ test cases follow.
The first line contains single integers ‘N’ represent the size of the list ‘BINARYNUMS’.
The second line contains ‘N’ space-separated string representing the list ‘BINARYNUMS’.
For each test case, print a single line containing a single string that represents this ‘Missing Integer’ in binary without leading zeros.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10 ^ 4
Where ‘T’ is the total number of test cases and ‘N’ is the size of list ‘BINARYNUMS’
Time limit: 1 sec.
In this approach, we will create an array of integers ‘nums’, by converting each string present in list ‘binaryNums’ in the integers they represent.
A binary string can be converted into a respective integer by simply traversing from rightmost character (least significant bit) to leftmost character (most significant bit) and for each position of set bit i.e (index where char is ‘1’) we add the power of 2 according to its position in the result.
Let’s name the method that does this conversion be convertToInt(binStr), where ‘binStr’ is the binary string whose integer equivalent is needed to be determined, then it can be implemented as follows:
We can convert any integer to binary by repeatedly dividing it by 2. Let’s name the method that converts an integer to binary string be convertToBin(num), where ‘num’ is the integer which we convert into a binary string, then it can be implemented as follows:
Algorithm
In this approach, we will create a boolean array ‘present’ of size ‘N + 1’, where present[i] will be true, only if the binary representation of integer ‘i’ is present in the list binaryNums.
We can fill the boolean array ‘present’ by simply traversing over the list binaryNums and convert each string present in it using method convertToInt() defined in the previous approach, and mark the corresponding index true. In the end, only one index remains false, which will be the missing integer.
Algorithm
Let ‘xorAll’ be the bitwise xor of all the integers from 0 to ‘N’ and ‘xorNum’ be the bitwise xor of all the integers represented by the strings in the list binaryNums. Then missing integer will be xorAll ^ xorNum, where ^ is the bitwise xor operator.
Algorithm
The number of bits required to represent an integer ‘N’ will be floor(logN) + 1 (log on base 2).
You can observe that number of integers between 0 to N (inclusive) that have the 0th-bit set will be floor((N + 1)/2).
The number of integers between 0 to N (inclusive) that have the 1st-bit set will be 2 * floor((N + 1)/4) + max(0, (N + 1) % 4 - 2)
The number of integers between 0 to N (inclusive) that have the 2nd-bit set will be 4 * floor((N + 1) / 8) + max(0, (N + 1) % 8 - 4)
In general, we can say that number of integers between 0 to N (inclusive that have ith bit-set will be 2 ^ i * floor((N + 1) / 2 ^ (i + 1)) + max(0, (N + 1) % 2 ^ (i + 1) - 2 ^ i).
Algorithm
Cakes
1-3 Palindrome
Max Prefix
Merge Two Sorted Arrays Without Extra Space
Sort 0s, 1s, 2s