Knowledge Guide
HomeDSARecursion

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 #InputExpected OutputDescription
102468Not GoodNumber 6 at index 3 is not a prime number.
223478GoodAll digits at even indices are even, and all digits at odd indices are prime. Therefore, the digit string is considered "good".
3224365GoodAll digits at even indices are even, and all digits at odd indices are prime. Therefore, the digit string is considered "good".

Solution:

  1. The isPrime function is used to check if a number is prime.
  2. The isGoodNumber function is a recursive function that checks each digit in the string. It takes two parameters: the digit string and the current index.
  3. In the isGoodNumber function, the base case is when the index reaches the end of the string, in which case it returns true.
  4. 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.
  5. If the conditions are not met, it returns false.
  6. Otherwise, it recursively calls isGoodNumber with the next index.
  7. In the main function, it tests the isGoodNumber function 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.

Image
Image

Code

Here is the code for this algorithm:

java
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 , where N is the length of the digit string. This is because the algorithm needs to check each digit in the string once. The space complexity is as well, considering the recursive stack space used during the function calls.

🤖 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.

🪜 Hint ladder (no spoilers)

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.
🎨 Explain the approach visually

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.
🔍 Review my solution

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.
🔁 Drill the pattern

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.

📝 My notes