Count of values chosen for X such that N is reduced to 0 after given operations | C ++ program for the above approach ; Function to check if the value of X reduces N to 0 or not ; Update the value of N as N - x ; Check if x is a single digit integer ; Function to find the number of values X such that N can be reduced to 0 after performing the given operations ; Number of digits in N ; Stores the count of value of X ; Iterate over all possible value of X ; Check if x follow the conditions ; Return total count ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }