Given an integer **N**, the task is to find the greatest number less than **N** such that the sum of its digits is greater than the sum of the digits of **N**. If the condition isn’t satisfied for any number then print **-1**.

**Examples:**

Input:N = 100

Output:99

99 is the largest number less than 100 sum of whose digits is greater than the sum of the digits of 100

Input:N = 49

Output:-1

**Approach:** Start a loop from **N-1** to **1** and check whether the sum of the digits of any number is greater than the sum of the digits of **N**. The first number that satisfies the condition is the required number.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the sum of the digits of n ` `int` `sumOfDigits(` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Loop for each digit of the number ` ` ` `while` `(n > 0) { ` ` ` `res += n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Function to return the greatest ` `// number less than n such that ` `// the sum of its digits is greater ` `// than the sum of the digits of n ` `int` `findNumber(` `int` `n) ` `{ ` ` ` ` ` `// Starting from n-1 ` ` ` `int` `i = n - 1; ` ` ` ` ` `// Check until 1 ` ` ` `while` `(i > 0) { ` ` ` ` ` `// If i satisfies the given condition ` ` ` `if` `(sumOfDigits(i) > sumOfDigits(n)) ` ` ` `return` `i; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// If the condition is not satisfied ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 824; ` ` ` `cout << findNumber(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to return the sum of the digits of n ` `static` `int` `sumOfDigits(` `int` `n) ` `{ ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// Loop for each digit of the number ` ` ` `while` `(n > ` `0` `) { ` ` ` `res += n % ` `10` `; ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Function to return the greatest ` `// number less than n such that ` `// the sum of its digits is greater ` `// than the sum of the digits of n ` `static` `int` `findNumber(` `int` `n) ` `{ ` ` ` ` ` `// Starting from n-1 ` ` ` `int` `i = n - ` `1` `; ` ` ` ` ` `// Check until 1 ` ` ` `while` `(i > ` `0` `) { ` ` ` ` ` `// If i satisfies the given condition ` ` ` `if` `(sumOfDigits(i) > sumOfDigits(n)) ` ` ` `return` `i; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// If the condition is not satisfied ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `n = ` `824` `; ` ` ` `System.out.println (findNumber(n)); ` ` ` `} ` `//This code is contributed by akt_mit ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the sum ` `# of the digits of n ` `def` `sumOfDigits(n) : ` ` ` ` ` `res ` `=` `0` `; ` ` ` ` ` `# Loop for each digit of the number ` ` ` `while` `(n > ` `0` `) : ` ` ` `res ` `+` `=` `n ` `%` `10` ` ` `n ` `/` `=` `10` ` ` ` ` `return` `res; ` ` ` `# Function to return the greatest ` `# number less than n such that ` `# the sum of its digits is greater ` `# than the sum of the digits of n ` `def` `findNumber(n) : ` ` ` ` ` `# Starting from n-1 ` ` ` `i ` `=` `n ` `-` `1` `; ` ` ` ` ` `# Check until 1 ` ` ` `while` `(i > ` `0` `) : ` ` ` ` ` `# If i satisfies the given condition ` ` ` `if` `(sumOfDigits(i) > sumOfDigits(n)) : ` ` ` `return` `i ` ` ` ` ` `i ` `-` `=` `1` ` ` ` ` `# If the condition is not satisfied ` ` ` `return` `-` `1` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `824` `; ` ` ` `print` `(findNumber(n)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to return the sum ` `// of the digits of n ` `static` `int` `sumOfDigits(` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Loop for each digit of ` ` ` `// the number ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `res += n % 10; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Function to return the greatest ` `// number less than n such that ` `// the sum of its digits is greater ` `// than the sum of the digits of n ` `static` `int` `findNumber(` `int` `n) ` `{ ` ` ` ` ` `// Starting from n-1 ` ` ` `int` `i = n - 1; ` ` ` ` ` `// Check until 1 ` ` ` `while` `(i > 0) ` ` ` `{ ` ` ` ` ` `// If i satisfies the given condition ` ` ` `if` `(sumOfDigits(i) > sumOfDigits(n)) ` ` ` `return` `i; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// If the condition is ` ` ` `// not satisfied ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 824; ` ` ` `Console.WriteLine (findNumber(n)); ` `} ` `} ` ` ` `// This code is contributed by @Tushil ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `//PHP implementation of the approach ` ` ` `// Function to return the sum of ` `// the digits of n ` `function` `sumOfDigits(` `$n` `) ` `{ ` ` ` `$res` `= 0; ` ` ` ` ` `// Loop for each digit of the number ` ` ` `while` `(` `$n` `> 0) ` ` ` `{ ` ` ` `$res` `+= ` `$n` `% 10; ` ` ` `$n` `/= 10; ` ` ` `} ` ` ` ` ` `return` `$res` `; ` `} ` ` ` `// Function to return the greatest ` `// number less than n such that ` `// the sum of its digits is greater ` `// than the sum of the digits of n ` `function` `findNumber(` `$n` `) ` `{ ` ` ` ` ` `// Starting from n-1 ` ` ` `$i` `= ` `$n` `- 1; ` ` ` ` ` `// Check until 1 ` ` ` `while` `(` `$i` `> 0) ` ` ` `{ ` ` ` ` ` `// If i satisfies the given condition ` ` ` `if` `(sumOfDigits(` `$i` `) > sumOfDigits(` `$n` `)) ` ` ` `return` `$i` `; ` ` ` `$i` `--; ` ` ` `} ` ` ` ` ` `// If the condition is not satisfied ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `$n` `= 824; ` ` ` `echo` `findNumber(` `$n` `); ` ` ` `// This code is contributed by Mukul singh ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

819

## Recommended Posts:

- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N whose each digit is prime number
- Largest even digit number not greater than N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Check if frequency of each digit is less than the digit
- Sum of largest prime factor of each number less than equal to n
- Largest subset having with sum less than equal to sum of respective indices
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Find Largest Special Prime which is less than or equal to a given number
- Largest number less than or equal to N/2 which is coprime to N
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Largest number M less than N such that XOR of M and N is even
- Minimum N-Digit number required to obtain largest N-digit number after performing given operations
- Count numbers with difference between number and its digit sum greater than specific value
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Largest number not greater than N all the digits of which are odd
- Largest number not greater than N which can become prime after rearranging its digits
- Find largest factor of N such that N/F is less than K
- Largest subsequence having GCD greater than 1
- Length of largest sub-array having primes strictly greater than non-primes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.