Count of values chosen for X such that N is reduced to 0 after given operations
Last Updated :
12 Jan, 2022
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:
- 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.
- Operation 2: Update N as N – X, N = 30 – 19 = 11, Update X as sum of digit of X, X = 9 + 1 = 10.
- Operation 3: Update N as N – X, N = 11 – 10 = 1, Update X as sum of digit of X, X = 1 + 0 = 1.
- 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++
#include <bits/stdc++.h>
using namespace std;
bool check( int x, int N)
{
while (1) {
N -= x;
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;
}
int countNoOfsuchX( int N)
{
int k = ceil ( log10 (N));
int count = 1;
for ( int x =( N - (k * (k + 1) * 5)); x < N; x++) {
if (check(x, N)) {
count += 1;
}
}
return count;
}
int main()
{
int N = 9399;
cout << countNoOfsuchX(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean check( int x, int N)
{
while ( true ) {
N -= x;
if (x < 10 )
break ;
int temp2 = 0 ;
while (x> 0 ) {
temp2 += (x % 10 );
x = ( int )x/ 10 ;
}
x = temp2;
}
if ((x < 10 ) && (N == 0 )) {
return true ;
}
return false ;
}
static int countNoOfsuchX( int N)
{
int k = ( int )(Math.log10(N))+ 1 ;
int count = 1 ;
for ( int x =( N - (k * (k + 1 ) * 5 )); x <= N; x++) {
if (check(x, N)) {
count += 1 ;
}
}
return count;
}
public static void main(String[] args)
{
int N = 9399 ;
System.out.println(countNoOfsuchX(N));
}
}
|
Python3
def check(x, N):
while True :
N - = x
if len ( str (x)) = = 1 :
break
x = sum ( list ( map ( int , str (x))))
if len ( str (x)) = = 1 and N = = 0 :
return 1
return 0
def countNoOfsuchX(N):
k = len ( str (N))
count = 0
for x in range (N - k * (k + 1 ) * 5 , N + 1 ):
if check(x, N):
count + = 1
return count
N = 9939
print (countNoOfsuchX(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool check( int x, int N)
{
while ( true ) {
N -= x;
if (x < 10)
break ;
int temp2 = 0;
while (x>0) {
temp2 += (x % 10);
x = ( int )x/10;
}
x = temp2;
}
if ((x < 10) && (N == 0)) {
return true ;
}
return false ;
}
static int countNoOfsuchX( int N)
{
int k = ( int )(Math.Log10(N))+1;
int count = 1;
for ( int x =( N - (k * (k + 1) * 5)); x <= N; x++) {
if (check(x, N)) {
count += 1;
}
}
return count;
}
public static void Main()
{
int N = 9399;
Console.Write(countNoOfsuchX(N));
}
}
|
Javascript
<script>
function check(x, N)
{
while ( true ) {
N -= x;
if (x < 10)
break ;
let temp2 = 0;
while (x>0) {
temp2 += (x % 10);
x = Math.floor(x / 10);
}
x = temp2;
}
if ((x < 10) && (N == 0)) {
return true ;
}
return false ;
}
function countNoOfsuchX(N)
{
let k = Math.floor(Math.log10(N)) + 1;
let count = 1;
for (let x =( N - (k * (k + 1) * 5)); x <= N; x++) {
if (check(x, N)) {
count += 1;
}
}
return count;
}
let N = 9399;
document.write(countNoOfsuchX(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...