VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-number-of-integers-that-can-be-obtained-by-replacing-in-given-string-for-perfect-divisibility-by-9/

⇱ Find number of integers that can be obtained by replacing ? in given string for perfect divisibility by 9 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find number of integers that can be obtained by replacing ? in given string for perfect divisibility by 9

Last Updated : 18 Jan, 2024

Given a String S of length N. S contains 'X' at some places. Then your task is to output the number distinct of integers that can be formed by replacing 'X' from the range [0, 9] and the formed integer leaves the remainder 0 after dividing them by 9.

Note: Numbers with leading 0s are not allowed to form.

Examples:

Input: N = 2, S = XX
Output: 10
Explanation: There can be 10 possible integers, If we use the digits from the range [0, 9] at both places of X. The 10 numbers will be: {18, 27, 36, 45, 54, 63, 72, 81, 90, 99}. They all leave remainder 0 when dividing by 9.

Input: N = 2, S = 9X
Output: 2
Explanation: It can be verified that there will be only two possible such integers. Which are 90 and 99.

Approach: Implement the idea below to solve the problem

The problem is observation based. Let us observe it.

There are 3 cases to observe the problem:

  • Case 1: If sum of all digit characters is divisible by 9. Then:
    • Example 1: 2X7X (Count of X = 2)
      • Possible Integers = 2070, 2079, 2178, 2277, 2376, 2475, 2574, 2673, 2772, 2871, 2970, 2979
      • Total: 12
    • Example 2: 2X7 (Count of X = 1)
      • Possible integers = 207 and 297
      • Total Possibilities = 2
    • Then the count of such integers is: (Count('X')-1) 1's followed by 2.
  • Case 2: If sum of all digit characters is not divisible by 9
    • Example 1: 2X5X (Count of X = 2)
      • Possible Integers = 2052, 2151, 2250, 2259, 2358, 2457, 2556, 2655, 2754, 2853, 2952
      • Total: 11
    • Example 2: 2X5 (Count of X = 1)
      • Possible Integers = 225
      • Total possibilities = 1
    • Then the count of such possible integers is: (Count('X')) 1s.
  • Case 3: If first character of string is 'X'
    • Please note that there can't be leading 0s.
    • Example 1: X27X (Count of X = 2)
      • Possible Integers = 1278, 2277, 3276, 4275, 5274, 6273, 7272, 8271, 9270, 9279
      • Total Possibilities = 10
    • Example 2: X5X (Count of X = 2)
      • Possible Integers = 153, 252, 351, 450, 459, 558, 657, 756, 855, 954
      • Total possibilities = 10
    • Then the count of such possible integers is: 1 then followed by (Count('X') - 1) 0's.

Step-by-step approach:

  • Create two variables let say sum and q to store the sum of integers in s and count of X respectively.
  • Run a loop on S and count X and calculate Sum
  • If (First Character is X)
    • Output 1 then (q-1) times 0s
  • If (Sum is divisible by 9)
    • Output 1q times
  • Else
    • Output 1 q-2 times and then 2 once.

Below is the implementation of the above approach:


Output
10

Time Complexity: O(N)
Auxiliary Space: O(1)

Comment