Similar Problems

Similar Problems not available

Numbers With Same Consecutive Differences - Leetcode Solution

Companies:

LeetCode:  Numbers With Same Consecutive Differences Leetcode Solution

Difficulty: Medium

Topics: backtracking breadth-first-search  

Problem Statement:

Given an integer n and a positive integer k, you need to check whether any pair of consecutive digits of the number n differ by at most k.

Return a boolean true if its possible, otherwise return false.

Example 1:

Input: n = 153, k = 1

Output: true

Explanation: The difference between 1 and 5 is 4 (which is less than k=1).

The difference between 5 and 3 is 2 (which is less than k=1).

Example 2:

Input: n = 1234567890, k = 1

Output: true

Explanation: Each two consecutive digits differ at most k=1.

Example 3:

Input: n = 120, k = 1

Output: false

Explanation: The difference between 2 and 0 is 2 which is greater than k=1.

Approach:

We will use a recursive approach to generate all numbers with the same consecutive digits.

We will start by taking the last digit of the input number n. And for each digit, we will try to add k and subtract k from it. If the resulting digit is between 0 and 9 (inclusive), we will append it to the number generated so far.

We will continue this process until we have generated a number with the same number of digits as the input number n. If this number is the same as the input number n, we can return true. Otherwise, we will repeat the process with the new number generated.

If we have gone through all the possible numbers that can be generated and none of them match the input number n, we can return false.

Solution:

We will create a recursive function that takes two arguments, the number generated so far (num), and the last digit of the input number (last_digit).

bool dfs(int num, int last_digit, int n, int k) {
    if (n == 0) return true;

    for (int i = 0; i <= 9; i++) {
        if (i == 0 && num == 0) continue; // avoid leading zero
        int diff = abs(last_digit - i);
        if (diff == k) {
            int new_num = num * 10 + i;
            if (dfs(new_num, i, n - 1, k)) return true;
        }
    }

    return false;
}

In the main function, we will call the recursive function with initial values of num = 0, last_digit = 0 (this is just a placeholder), and n = number of digits in the input number.

bool numsSameConsecDiff(int n, int k) {
    if (n == 1) return true; // single digit number always satisfies the condition
    
    bool res = false;
    for (int i = 1; i <= 9; i++) {
        res |= dfs(i, i, n - 1, k);
    }
    
    return res;
}

We will iterate over all possible starting digits (1 to 9) and call the recursive function with it. If any of the calls return true, we can return true, otherwise, we can return false.

Complexity Analysis:

Time Complexity: O(2^N) where N is the number of digits in the input number. This is because at each digit, we have two choices (add k or subtract k).

Space Complexity: O(N) for the recursive stack space.

Numbers With Same Consecutive Differences Solution Code

1