You are given an initial string s starting with “0”. The string keeps duplicating as follows. Invert of it is appended to it.
Input : k = 2 Output : 1 Initially s = "0". First Iteration : s = s + s' = "01" Second Iteration : s = s + s' = "0110" The digit at index 2 of s is 1. Input : k = 12 Output : 0
1. Naive Approach
We can build the string s while its length is smaller than or equal to i in the manner mentioned in the problem description and then simply do a lookup of the required index in the string s.
Time Complexity: O(k log k).
Let’s take a look at a few string constructions:
1. s = 0
2. s = 01
3. s = 0110
4. s = 01101001
5. s = 0110100110010110
Let’s consider finding the bit at position 11 in Line 5. The bit value at this position is effectively the complement of the bit at index 3 in Line 4. So we effectively need to find the complement of the bit at index 3.
s = ~(s). However we do not know s either. Let’s move ahead. Now s = ~(s) using the same explanation in Line 3. And s = ~(s). However, s is always 0 from the problem statement.
Plugging these results,
s = ~(~(~0)) = 1 where ‘~’ is the bitwise NOT operator.
Now, k was initially 11 which is 1011 in binary. The next value of k was 3 that is 011. Notice how the first set bit has reduced from the original k. Subsequently, the next value of k is 1. Another set bit has reduced. Finally for k = 0, the last set bit has vanished. So for k = 11 which is 1011 in binary, the number of complements are 3, which is incidentally equal to the number of set bits in 11. Now we see for odd number of inversions the final result is 1. We can derive the same reasoning for even number of inversions yielding a final result as 0.
To count the number of set-bits in an integer, refer to this article – Count set bits in an integer.
Time Complexity: O(log k)
This article is contributed by Aneesh Bose. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Impact going to be created by Augmented Reality and Virtual reality
- Endian order and binary files
- Best meeting point in 2D binary array
- Rethinking binary with Quantum computers
- Tutorial on Binary Search Tree
- Minimum sum of squares of character counts in a given string after removing k characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Find maximum (or minimum) sum of a subarray of size k
- Find product of prime numbers between 1 to n
- Find the only different element in an array
- Find all divisors of a natural number | Set 2
- Find all divisors of a natural number | Set 1
- Find if given number is sum of first n natural numbers
- Find number of days between two given dates
- Univariate, Bivariate and Multivariate data and its analysis
- Look-and-Say Sequence
- How to add articles to "To Do" and "Done" lists on GeeksforGeeks?
- Finding the number of triangles amongst horizontal and vertical line segments
- MouseListener and MouseMotionListener in Java
- Shuffle a pack of cards and answer the query