Solution Good Number
Problem Statement
Write a Recursive Approach to Check if a Given Digit String Represents a Good Number.
A digit string is good if the digits (0-indexed) at even indices are even and the digits at odd indices are prime ((2, 3, 5, or 7).
Examples:
| Sr # | Input | Expected Output | Description |
|---|---|---|---|
| 1 | 02468 | Not Good | Number 6 at index 3 is not a prime number. |
| 2 | 23478 | Good | All digits at even indices are even, and all digits at odd indices are prime. Therefore, the digit string is considered "good". |
| 3 | 224365 | Good | All digits at even indices are even, and all digits at odd indices are prime. Therefore, the digit string is considered "good". |
Solution:
- The
isPrimefunction is used to check if a number is prime. - The
isGoodNumberfunction is a recursive function that checks each digit in the string. It takes two parameters: the digit string and the current index. - In the
isGoodNumberfunction, the base case is when the index reaches the end of the string, in which case it returnstrue. - Inside the recursive function, it checks if the digit at the current index satisfies the conditions: even index digits must be even and odd index digits must be prime.
- If the conditions are not met, it returns
false. - Otherwise, it recursively calls
isGoodNumberwith the next index. - In the
mainfunction, it tests theisGoodNumberfunction on the example digit strings and prints the result.
This code uses recursion to traverse each digit in the string and checks if it satisfies the conditions for a "good number". The base case ensures that the recursion stops when all digits have been checked. By checking each digit recursively, the code can determine if the digit string is "good" or not.
Code
Here is the code for this algorithm:
import java.util.Arrays;
import java.util.List;
public class Solution {
public static boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
public static boolean isGoodNumber(String digits, int index) {
if (index >= digits.length()) return true; // Base case: reached the end of the string
char digit = digits.charAt(index);
// Check if the digit at the current index satisfies the conditions
if (
(index % 2 == 0 && (digit - '0') % 2 != 0) ||
(index % 2 != 0 && !isPrime(digit - '0'))
) return false;
// Recursively check the next digit
return isGoodNumber(digits, index + 1);
}
public static void main(String[] args) {
// Example inputs
List<String> digitStrings = Arrays.asList("02468", "23478", "123456");
for (String digits : digitStrings) {
boolean isGood = isGoodNumber(digits, 0);
System.out.println(
"Digit string: " + digits + " is " + (isGood ? "good" : "not good")
);
}
}
}
Space and Time Complexity
The time complexity of this algorithm is
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Good Number? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Good Number** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Good Number** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Good Number**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Good Number**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.