Find a Number X whose sum with its digits is equal to N
Last Updated :
15 Nov, 2023
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 equal to N – sum of digits of X. All the possibilities will be covered in this range.
C++
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int sumOfDigits( long int x)
{
int sum = 0;
while (x > 0) {
sum += x % 10;
x /= 10;
}
return sum;
}
void compute(vector< long int >& answer, long int n)
{
for ( int i = 0; i <= 100; i++) {
long int valueOnLeft = abs (n - i) +
sumOfDigits( abs (n - i));
long int valueOnRight = n + i + sumOfDigits(n + i);
if (valueOnLeft == n)
answer.push_back( abs (n - i));
if (valueOnRight == n)
answer.push_back(n + i);
}
}
int main()
{
long int N = 100000001;
vector< long int > answer;
compute(answer, N);
if (answer.size() == 0)
cout << -1;
else {
for ( auto it = answer.begin(); it != answer.end(); ++it)
cout << "X = " << (*it) << endl;
}
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GeeksforGeeks {
static int sumOfDigits( long x)
{
int sum = 0 ;
while (x > 0 ) {
sum += (x % 10 );
x /= 10 ;
}
return sum;
}
static void compute( long n)
{
long answer[] = new long [ 100 ];
int pos = 0 ;
for ( int i = 0 ; i <= 100 ; i++) {
long valueOnLeft = Math.abs(n - i) +
sumOfDigits(Math.abs(n - i));
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]);
}
public static void main(String[] args)
{
long N = 100000001 ;
compute(N);
}
}
|
Python3
def sumOfDigits(x):
sum = 0 ;
while (x > 0 ):
sum + = (x % 10 );
x = int (x / 10 );
return sum ;
def compute(n):
answer = [];
pos = 0 ;
for i in range ( 101 ):
valueOnLeft = ( abs (n - i) +
sumOfDigits( abs (n - i)));
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]);
N = 100000001 ;
compute(N);
|
C#
using System;
public class GFG{
static int sumOfDigits( long x)
{
int sum = 0;
while (x > 0) {
sum += ( int )(x % 10);
x /= 10;
}
return sum;
}
static void compute( long n)
{
long []answer = new long [100];
int pos = 0;
for ( int i = 0; i <= 100; i++) {
long valueOnLeft = Math.Abs(n - i) +
sumOfDigits(Math.Abs(n - i));
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]);
}
static public void Main (){
long N = 100000001;
compute(N);
}
}
|
Javascript
<script>
function sumOfDigits(x)
{
let sum = 0;
while (x > 0)
{
sum += (x % 10);
x = Math.floor(x / 10);
}
return sum;
}
function compute(n)
{
let answer = [];
let pos = 0;
for (let i = 0; i <= 100; i++)
{
let valueOnLeft = Math.abs(n - i) +
sumOfDigits(Math.abs(n - i));
let 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)
document.write(-1);
else
for (let i = 0; i < pos; i++)
document.write( "X = " + answer[i] + "<br/>" );
}
let N = 100000001;
compute(N);
</script>
|
PHP
<?php
function sumOfDigits( $x )
{
$sum = 0;
while ( $x > 0)
{
$sum += ( $x % 10);
$x = (int) $x / 10;
}
return $sum ;
}
function compute( $n )
{
$answer = array (0);
$pos = 0;
for ( $i = 0; $i <= 100; $i ++)
{
$valueOnLeft = abs ( $n - $i ) +
sumOfDigits( abs ( $n - $i ));
$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" ;
}
$N = 100000001;
compute( $N );
?>
|
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
Approach#2: Using binary search
this approach to solve this problem is to use binary search. We can start with the middle value between 1 and N, and then we can check if the sum of this number and its digit sum is equal to N. If it is not, we can use binary search to find a number that satisfies the condition.
Algorithm
1. Define a function “find_number_with_digit_sum” that takes an integer “N” as input.
2. Define a function “check_number_with_digit_sum” that takes a number “X” and an integer “N” as input, and checks if the sum of “X” and its digit sum is equal to “N”. Return True if the condition is satisfied, False otherwise.
3. Set “low” to 1 and “high” to N.
4. While “low” is less than or equal to “high”:
a. Calculate the middle value “mid” between “low” and “high”.
b. If “check_number_with_digit_sum(mid, N)” returns True, return “mid”.
c. If “mid” plus its digit sum is less than “N”, set “low” to “mid” + 1.
d. Otherwise, set “high” to “mid” – 1.
5. If no number was found, return -1.
C++
#include <iostream>
using namespace std;
int digit_sum( int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
bool check_number_with_digit_sum( int X, int N) {
return X + digit_sum(X) == N;
}
int find_number_with_digit_sum( int N) {
int low = 1, high = N;
while (low <= high) {
int mid = (low + high) / 2;
if (check_number_with_digit_sum(mid, N)) {
return mid;
}
else if (mid + digit_sum(mid) < N) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
int main() {
int N = 100000001;
cout << find_number_with_digit_sum(N) << endl;
return 0;
}
|
Java
public class Main {
static int digitSum( int n) {
int sum = 0 ;
while (n > 0 ) {
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
static boolean checkNumberWithDigitSum( int X, int N) {
return X + digitSum(X) == N;
}
static int findNumberWithDigitSum( int N) {
int low = 1 , high = N;
while (low <= high) {
int mid = (low + high) / 2 ;
if (checkNumberWithDigitSum(mid, N)) {
return mid;
} else if (mid + digitSum(mid) < N) {
low = mid + 1 ;
} else {
high = mid - 1 ;
}
}
return - 1 ;
}
public static void main(String[] args) {
int N = 100000001 ;
System.out.println(findNumberWithDigitSum(N));
}
}
|
Python3
def digit_sum(n):
sum = 0
while n > 0 :
sum + = n % 10
n / / = 10
return sum
def check_number_with_digit_sum(X, N):
return X + digit_sum(X) = = N
def find_number_with_digit_sum(N):
low, high = 1 , N
while low < = high:
mid = (low + high) / / 2
if check_number_with_digit_sum(mid, N):
return mid
elif mid + digit_sum(mid) < N:
low = mid + 1
else :
high = mid - 1
return - 1
N = 100000001
print (find_number_with_digit_sum(N))
|
C#
using System;
class Program
{
static int DigitSum( int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
static bool CheckNumberWithDigitSum( int X, int N)
{
return X + DigitSum(X) == N;
}
static int FindNumberWithDigitSum( int N)
{
int low = 1, high = N;
while (low <= high)
{
int mid = (low + high) / 2;
if (CheckNumberWithDigitSum(mid, N))
{
return mid;
}
else if (mid + DigitSum(mid) < N)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
static void Main()
{
int N = 100000001;
Console.WriteLine(FindNumberWithDigitSum(N));
}
}
|
Javascript
function digitSum(n) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function checkNumberWithDigitSum(X, N) {
return X + digitSum(X) === N;
}
function findNumberWithDigitSum(N) {
let low = 1;
let high = N;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (checkNumberWithDigitSum(mid, N)) {
return mid;
} else if (mid + digitSum(mid) < N) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
const N = 100000001;
console.log(findNumberWithDigitSum(N));
|
Time Complexity: O(log10(N) * log10(X)), where X is the answer.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...