Related Articles

# Find a Number X whose sum with its digits is equal to N

• Difficulty Level : Hard
• Last Updated : 22 Apr, 2021

Given a positive number N. We need to find number(s) such that sum of digits of those numbers to themselves is equal to N. If no such number is possible print -1. Here N Examples:

Input : N = 21
Output : X = 15
Explanation : X + its digit sum
= 15 + 1 + 5
= 21

Input  : N = 5
Output : -1

Input : N = 100000001
Output : X = 99999937
X = 100000000

Method 1 : (Naive Approach)
We have already discussed the approach here. The approach might not work for N as large as .

Method 2 : (Efficient)
It is a fact that for a number X < = 1000000000, the sum of digits never exceeds 100. Using this piece of information, we can iterate over all possibilities in the range 0 to 100 on both the sides of the number and check if the number X is eqaul to N – sum of digits of X. All the possibilities will be covered in this range.

## C++

 // CPP program to find x such that// X + sumOfDigits(X) = N#include #include #include #include using namespace std; // Computing the sum of digits of xint sumOfDigits(long int x){    int sum = 0;    while (x > 0) {        sum += x % 10;        x /= 10;    }    return sum;} // Checks for 100 numbers on both left// and right side of the given number to// find such numbers X such that X +// sumOfDigits(X) = N and updates the answer// vector accordinglyvoid compute(vector<long int>& answer, long int n){    // Checking for all possibilities of    // the answer    for (int i = 0; i <= 100; i++) {         // Evaluating the value on the left        // side of the given number        long int valueOnLeft = abs(n - i) +                      sumOfDigits(abs(n - i));         // Evaluating the value on the right        // side of the given number        long int valueOnRight = n + i + sumOfDigits(n + i);         // Checking the condition of equality        // on both sides of the given number N        // and updating the answer vector        if (valueOnLeft == n)            answer.push_back(abs(n - i));        if (valueOnRight == n)            answer.push_back(n + i);    }} // Driver Functionint main(){       long int N = 100000001;     vector<long int> answer;    compute(answer, N);     // If no solution exists, print -1    if (answer.size() == 0)        cout << -1;    else {         // If one or more solutions are possible,        // printing them!        for (auto it = answer.begin(); it != answer.end(); ++it)            cout << "X = " << (*it) << endl;    }    return 0;}

## Java

 // Java program to find x such that// X + sumOfDigits(X) = Nimport java.util.*;import java.lang.*;import java.io.*; class GeeksforGeeks {     // Computing the sum of digits of x    static int sumOfDigits(long x)    {        int sum = 0;        while (x > 0) {            sum += (x % 10);            x /= 10;        }        return sum;    }     // Checks for 100 numbers on both left    // and right side of the given number to    // find such numbers X such that    // X + sumOfDigits(X) = N and prints solution.    static void compute(long n)    {        long answer[] = new long;        int pos = 0;         // Checking for all possibilities of the answer        // in the given range        for (int i = 0; i <= 100; i++) {             // Evaluating the value on the left side of the            // given number            long valueOnLeft = Math.abs(n - i) +                               sumOfDigits(Math.abs(n - i));             // Evaluating the value on the right side of the            // given number            long valueOnRight = (n + i) + sumOfDigits(n + i);             if (valueOnRight == n)                answer[pos++] = (n + i);            if (valueOnLeft == n)                answer[pos++] = Math.abs(n - i);        }         if (pos == 0)            System.out.print(-1);        else            for (int i = 0; i < pos; i++)                System.out.println("X = " + answer[i]);    }    // Driver Function    public static void main(String[] args)    {        long N = 100000001;        compute(N);    }}

## Python3

 # Python3 program to find x such that# X + sumOfDigits(X) = N # Computing the sum of digits of xdef sumOfDigits(x):     sum = 0;    while (x > 0):        sum += (x % 10);        x = int(x / 10);    return sum; # Checks for 100 numbers on both left# and right side of the given number# to find such numbers X such that# X + sumOfDigits(X) = N and prints# solution.def compute(n):     answer = [];    pos = 0;     # Checking for all possibilities    # of the answer in the given range    for i in range(101):         # Evaluating the value on the        # left side of the given number        valueOnLeft = (abs(n - i) +                       sumOfDigits(abs(n - i)));         # Evaluating the value on the right        # side of the given number        valueOnRight = (n + i) + sumOfDigits(n + i);         if (valueOnRight == n):            answer.append(n + i);        if (valueOnLeft == n):            answer.append(abs(n - i));     if (len(answer)== 0):        print(-1);    else:        for i in range(len(answer)):            print("X =", answer[i]);             # Driver CodeN = 100000001;compute(N); # This code is contributed# by mits

## C#

 // C# program to find x such that// X + sumOfDigits(X) = N using System; public class GFG{    // Computing the sum of digits of x    static int sumOfDigits(long x)    {        int sum = 0;        while (x > 0) {            sum += (int)(x % 10);            x /= 10;        }        return sum;    }     // Checks for 100 numbers on both left    // and right side of the given number to    // find such numbers X such that    // X + sumOfDigits(X) = N and prints solution.    static void compute(long n)    {        long []answer = new long;        int pos = 0;         // Checking for all possibilities of the answer        // in the given range        for (int i = 0; i <= 100; i++) {             // Evaluating the value on the left side of the            // given number            long valueOnLeft = Math.Abs(n - i) +                            sumOfDigits(Math.Abs(n - i));             // Evaluating the value on the right side of the            // given number            long valueOnRight = (n + i) + sumOfDigits(n + i);             if (valueOnRight == n)                answer[pos++] = (n + i);            if (valueOnLeft == n)                answer[pos++] = Math.Abs(n - i);        }         if (pos == 0)            Console.Write(-1);        else            for (int i = 0; i < pos; i++)                Console.WriteLine("X = " + answer[i]);    }    // Driver Function              static public void Main (){        long N = 100000001;        compute(N);    }}

## PHP

  0)    {        $sum += ($x % 10);        $x = (int)$x / 10;    }    return $sum;} // Checks for 100 numbers on both left// and right side of the given number// to find such numbers X such that// X + sumOfDigits(X) = N and prints// solution.function compute($n){    $answer = array(0); $pos = 0;     // Checking for all possibilities    // of the answer in the given range    for ($i = 0; $i <= 100; $i++) {  // Evaluating the value on the // left side of the given number $valueOnLeft = abs($n - $i) +                        sumOfDigits(abs($n - $i));         // Evaluating the value on the right        // side of the given number        $valueOnRight = ($n + $i) + sumOfDigits($n + $i);  if ($valueOnRight == $n) $answer[$pos++] = ($n + $i); if ($valueOnLeft == $n) $answer[$pos++] =abs($n - $i); }  if ($pos == 0)        echo (-1),"\n";    else        for ($i = 0; $i < $pos; $i++)            echo "X = ", $answer[$i], "\n";             } // Driver Code$N = 100000001;compute($N); // This code is contributed// by Sach_Code?>

## Javascript

 

Output:

X = 100000000
X = 99999937

The maximum complexity of this approach can be where len is the number of digits in the number max(len) = 9. Thus the complexity can almost be said to be Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up