Open In App

Count of values chosen for X such that N is reduced to 0 after given operations

Last Updated : 12 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a positive integer N, the task is to find the number of integers X such that after performing the following sequence of operations reduces the value of N to 0.

  • Subtract the value of X from N.
  • If X is a single-digit integer, then stop applying operations.
  • Update the value of X as the sum of digits of X.

Examples:

Input: N = 9940
Output: 1
Explanation:
Considering the value of  X  as 9910, the value of N can be reduced to 0 as:

  1. Operation 1: Update N as N – X, N = 9939 – 9910 = 30, Update X as sum of digit of X, X = 9 + 9 + 1 + 0 = 19.
  2. Operation 2:  Update N as N – X, N = 30 – 19 = 11, Update X as sum of digit of X, X = 9 + 1 = 10.
  3. Operation 3: Update N as N – X, N = 11 – 10 = 1, Update X as sum of digit of X, X = 1 + 0 = 1.
  4. Operation 4:  Update N as N – X, N = 1 – 1 = 0, Stop applying operations.

Therefore, there exists only one value of X as 1.

Input: N = 9939
Output: 3

Approach: The given problem can be solved using the Greedy Approach which is based on the following observations:

  • It can be observed that choosing the value of X greater than N doesn’t reduce N to 0.
  • It can be surely said that if the number of digits in N is K, then no value of X is less than (N – 10 * K * (K + 1) / 2) and can convert N into 0.

From the above observations, the idea is to iterate over the range [N – 10 * K * (K + 1) / 2, N] and count those values in this range that can convert N into 0 after performing the given operations.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the value of X
// reduces N to 0 or not
bool check(int x, int N)
{
 
    while (1) {
 
       // Update the value of N as N-x
        N -= x;
 
      // Check if x is a single digit integer
        if (x < 10)
            break;
 
        int temp2 = 0;
        while (x) {
            temp2 *= 10;
            temp2 += (x % 10);
            x /= 10;
        }
        x = temp2;
    }
    if ((x < 10) && (N == 0)) {
        return 1;
    }
    return 0;
}
 
// Function to find the number of values
// X such that N can be reduced to 0
// after performing the given operations
int countNoOfsuchX(int N)
{
   
  // Number of digits in N
    int k = ceil(log10(N));
 
    // Stores the count of value of X
    int count = 1;
 
    // Iterate over all possible value
    // of X
    for (int x =( N - (k * (k + 1) * 5)); x < N; x++) {
      // Check if x follow the conditions
        if (check(x, N)) {
            count += 1;
        }
    }
   
  // Return total count
    return count;
}
 
// Driver code
int main()
{
    int N = 9399;
    cout << countNoOfsuchX(N);
    return 0;
}
 
// This code is contributed by maddler.


Java




// Java program for the above approach
import java.util.*;
 
class GFG {
 
// Function to check if the value of X
// reduces N to 0 or not
static boolean check(int x, int N)
{
 
    while (true) {
 
       // Update the value of N as N-x
        N -= x;
 
      // Check if x is a single digit integer
        if (x < 10)
            break;
 
        int temp2 = 0;
        while (x>0) {
           // temp2 *= 10;
            temp2 += (x % 10);
            x = (int)x/10;
        }
        x = temp2;
    }
    if ((x < 10) && (N == 0)) {
        return true;
    }
    return false;
}
 
// Function to find the number of values
// X such that N can be reduced to 0
// after performing the given operations
static int countNoOfsuchX(int N)
{
   
  // Number of digits in N
    int k = (int)(Math.log10(N))+1;
 
    // Stores the count of value of X
    int count = 1;
 
    // Iterate over all possible value
    // of X
    for (int x =( N - (k * (k + 1) * 5)); x <= N; x++) {
 
      // Check if x follow the conditions
        if (check(x, N)) {
            count += 1;
        }
    }
   
  // Return total count
    return count;
}
 
    // Driver Code
    public static void main(String[] args)
    {
         int N = 9399;
        System.out.println(countNoOfsuchX(N));
    }
}
 
// This code is contributed by sanjoy_2.


Python3




# Python program for the above approach
 
# Function to check if the value of X
# reduces N to 0 or not
def check(x, N):
 
    while True:
 
        # Update the value of N as N-x
        N -= x
 
        # Check if x is a single digit integer
        if len(str(x)) == 1:
            break
 
        # Update x as sum digit of x
        x = sum(list(map(int, str(x))))
 
    if len(str(x)) == 1 and N == 0:
 
        return 1
 
    return 0
 
# Function to find the number of values
# X such that N can be reduced to 0
# after performing the given operations
def countNoOfsuchX(N):
 
    # Number of digits in N
    k = len(str(N))
 
    # Stores the count of value of X
    count = 0
 
    # Iterate over all possible value
    # of X
    for x in range(N - k*(k + 1)*5, N + 1):
        # Check if x follow the conditions
        if check(x, N):
            count += 1
 
    # Return the total count
    return count
 
 
# Driver Code
N = 9939
print(countNoOfsuchX(N))


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to check if the value of X
// reduces N to 0 or not
static bool check(int x, int N)
{
 
    while (true) {
 
       // Update the value of N as N-x
        N -= x;
 
      // Check if x is a single digit integer
        if (x < 10)
            break;
 
        int temp2 = 0;
        while (x>0) {
           // temp2 *= 10;
            temp2 += (x % 10);
            x = (int)x/10;
        }
        x = temp2;
    }
    if ((x < 10) && (N == 0)) {
        return true;
    }
    return false;
}
 
// Function to find the number of values
// X such that N can be reduced to 0
// after performing the given operations
static int countNoOfsuchX(int N)
{
   
  // Number of digits in N
    int k = (int)(Math.Log10(N))+1;
 
    // Stores the count of value of X
    int count = 1;
 
    // Iterate over all possible value
    // of X
    for (int x =( N - (k * (k + 1) * 5)); x <= N; x++) {
 
      // Check if x follow the conditions
        if (check(x, N)) {
            count += 1;
        }
    }
   
  // Return total count
    return count;
}
 
// Driver code
public static void Main()
{
    int N = 9399;
    Console.Write(countNoOfsuchX(N));
}
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check if the value of X
// reduces N to 0 or not
function check(x, N)
{
  
    while (true) {
  
       // Update the value of N as N-x
        N -= x;
  
      // Check if x is a single digit integer
        if (x < 10)
            break;
  
        let temp2 = 0;
        while (x>0) {
           // temp2 *= 10;
            temp2 += (x % 10);
            x = Math.floor(x / 10);
        }
        x = temp2;
    }
    if ((x < 10) && (N == 0)) {
        return true;
    }
    return false;
}
  
// Function to find the number of values
// X such that N can be reduced to 0
// after performing the given operations
function countNoOfsuchX(N)
{
    
  // Number of digits in N
    let k = Math.floor(Math.log10(N)) + 1;
  
    // Stores the count of value of X
    let count = 1;
  
    // Iterate over all possible value
    // of X
    for (let x =( N - (k * (k + 1) * 5)); x <= N; x++) {
  
      // Check if x follow the conditions
        if (check(x, N)) {
            count += 1;
        }
    }
    
  // Return total count
    return count;
}
 
// Driver Code
     let N = 9399;
     document.write(countNoOfsuchX(N));
 
// This code is contributed by avijitmondal1998.
</script>


 
 

Output: 

3

 

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads