In this article, we will solve the problem of binary numbers. Binary numbers are sequences composed of 0s and 1s, which must satisfy certain conditions. This problem is one of the frequently asked questions in Java coding tests and can be solved using dynamic programming.
1. Problem Definition
Problem: Write a program to calculate the number of binary numbers of length N. Binary numbers must satisfy the following conditions:
- The first digit of the number must be 1.
- It must be composed only of 0s and 1s.
- No two consecutive 1s are allowed.
For example, the 3-digit binary numbers are 100, 101, and 110. The 4-digit binary numbers are 1000, 1001, 1010, 1100, and 1101. Let’s explore how to solve this problem.
2. Approach
To solve this problem, we can use a dynamic programming (DP) approach. The ways to create a binary number of length N can be structured as follows:
2.1. State Definition
A binary number can be divided into two cases based on whether the last digit is 0 or 1. Thus, we define the following two DP arrays.
dp0[i]
: the number of binary numbers of lengthi
whose last digit is 0dp1[i]
: the number of binary numbers of lengthi
whose last digit is 1
At this point, the total number of binary numbers of length N can be expressed as dp0[N] + dp1[N]
. By discovering the rules for constructing binary numbers, we can derive the following recurrence relation:
2.2. Recurrence Relation
dp0[i] = dp0[i-1] + dp1[i-1]
(sum of the counts of binary numbers of length i-1 ending with 0 and 1)dp1[i] = dp0[i-1]
(only binary numbers of length i-1 ending with 0 are possible)
2.3. Initial Conditions
The initial values are as follows:
dp0[1] = 0
(there are no one-digit numbers ending with 0)dp1[1] = 1
(there is one one-digit number ending with 1: 1)
3. Java Code Implementation
Now, let’s implement this in Java using these conditions.
public class BinaryNumbers {
private static int[] dp0;
private static int[] dp1;
public static void main(String[] args) {
int N = 4; // Input: N digits
dp0 = new int[N + 1];
dp1 = new int[N + 1];
// Set initial values
dp0[1] = 0;
dp1[1] = 1;
// Fill the DP table
for (int i = 2; i <= N; i++) {
dp0[i] = dp0[i - 1] + dp1[i - 1];
dp1[i] = dp0[i - 1];
}
// Print the result
int result = dp0[N] + dp1[N];
System.out.println("Number of binary numbers of length N: " + result);
}
}
The above code shows the overall structure of the program to find binary numbers. It fills the DP table at each step and outputs the result at the end.
4. Performance Analysis
The time complexity of this algorithm is O(N), and the space complexity is also O(N). This provides a very efficient way to calculate binary numbers, allowing for quick execution even for large N. The algorithm makes good use of dynamic programming and recurrence relations, and thus can be applied to many other similar problems.
5. Problem Variations
This problem can be modified to create various other problems. For example:
- A program that returns an array of binary numbers of length N
- A program that prints all possible combinations of binary numbers
- A program that checks whether a given sequence is a binary number
6. Conclusion
Today we covered how to find binary numbers. Through this, I hope to enhance the understanding of dynamic programming concepts and improve problem-solving skills using this pattern. It will be useful in future coding tests and algorithm problem-solving.