Find K-th Bit in N-th Binary String in Java13 May 2025 | 3 min read Problem StatementGiven two integers n and k. The problem generates a sequence where each Sn is formed recursively based on the previous string. The transformation follows the following pattern:
where:
The task is to find the kth bit in Sn without explicitly constructing the entire string due to its exponential growth. Example 1: Input: n = 4, k = 11 Output: 1 Explanation: S₄ = "011100110110001", and the 11th bit is '1'. It maps to position 5 in S₃ and gets inverted. Example 2: Input: n = 2, k = 3 Output: 1 Explanation: S₂ = "0110", and the 3rd bit is '1'. Since 3 is the middle position, we return '1'. Approach 1: Recursive Simulation ApproachThe approach recursively without explicitly constructing the entire string Sn. It generates the sequence; we determine the position of k based on the recursive properties of the transformation. AlgorithmStep 1: If n=1n, return '0', as S1 = "0". Step 2: The length of Sn is (2n−1). Step 3: The middle bit is always at position (length/2) + 1 and is '1'. Step 4: If k is in the left half (k < mid), it is the same as Sn−1. Step 5: If k is in the right half (k > mid), it maps to (length – k + 1) in Sn−1 but is inverted. Step 6: Solve for the new mapped index. Let’s implement the above steps in a Java program. Output: 1 ComplexityTime Complexity: The time complexity of a program is O(n). This is because each recursive call reduces n by 1, leading to at most n recursive calls. Space Complexity: The space complexity of a program is O(n). This is because of recursive function calls stored in the call stack. Next Topicnull |
? Static code blocks in Java are unique sections of code that are only run once, during the initialization of a class. They are typically used to execute one-time setup operations like initialising static variables or any other necessary setup. Static blocks are automatically executed by the...
3 min read
In this section, we will learn what is a bouncy number and also create Java programs to check if the given number is bouncy. The bouncy number program frequently asked in Java coding tests and academics. Before understanding the bouncy number, first, we will understand what...
4 min read
The 2048 game has captivated hundreds of thousands of players worldwide with its addictive nature and puzzling challenges. In this article, we can delve into the sector of 2048 and present a Java-primarily based implementation of the game. Additionally, we can explore effective techniques that...
6 min read
The java.nio.charset contains a built-in method called averageBytesPerChar(). CharsetEncoder gives back the average number of bytes that will be generated for each character of input. For a given input sequence, the heuristic value is used to determine the size of the output buffer that is needed....
2 min read
In this section, we will learn what is Tribonacci number and also create Java programs to that calculates the Tribonacci number. The Tribonacci number program is frequently asked in Java coding interviews and academics. Tribonacci Number Tribonacci numbers are the same as Fibonacci numbers. We can get the...
3 min read
In Java, generics are majorly used for providing a method for the creation of classes and methods which are capable of working with any type of data including the type safety. When generics are utilized in Java, the type of an object is often checked during the...
9 min read
In the world of concurrent programming, managing shared data and ensuring thread safety are crucial aspects. Java, being a popular programming language, provides robust features to handle concurrency. One such concept is the Concurrent Array, which allows multiple threads to access and modify elements concurrently without...
4 min read
Generating a random string in Java is a simple concept which is often required to build Ids, temporary passwords, session tokens or any other case where the alphanumeric string is most electronically required. There are several ways to achieve this using the different classes and...
13 min read
C Language C is a middle-level, compiled and general-purpose programming language that follows a top-down approach for developing applications. It was developed at Bell Labs by Dennis Ritchie in 1970 for the Unix Operating System 1970. It is ideal for developing firmware and portable applications. Example #include ...
4 min read
Complex numbers consist of two components - a real number and an imaginary number, which are distinct from each other. These numbers are widely used in mathematics, particularly in algebra. The standard format for a complex number is a + bi, where "a" represents the real...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India