Largest number less than N with digit sum greater than the digit sum of N
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
Brute Force Approach:
To solve this problem we can start iterating from N-1 down to 1, and for each number, compute the sum of its digits using a loop. Then, we can compare this sum to the sum of the digits of N, and if it is greater, we have found the answer and we can return the current number. If the loop completes without finding a suitable number, we can return -1.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int findNumber( int n)
{
for ( int i = n - 1; i > 0; i--) {
int sum_i = 0, sum_n = 0;
int temp_i = i, temp_n = n;
while (temp_i > 0) {
sum_i += temp_i % 10;
temp_i /= 10;
}
while (temp_n > 0) {
sum_n += temp_n % 10;
temp_n /= 10;
}
if (sum_i > sum_n) {
return i;
}
}
return -1;
}
int main()
{
int n = 824;
cout << findNumber(n);
return 0;
}
|
Java
import java.util.*;
class Main {
static int findNumber( int n)
{
for ( int i = n - 1 ; i > 0 ; i--) {
int sum_i = 0 , sum_n = 0 ;
int temp_i = i, temp_n = n;
while (temp_i > 0 ) {
sum_i += temp_i % 10 ;
temp_i /= 10 ;
}
while (temp_n > 0 ) {
sum_n += temp_n % 10 ;
temp_n /= 10 ;
}
if (sum_i > sum_n) {
return i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int n = 824 ;
System.out.println(findNumber(n));
}
}
|
Python3
def findNumber(n):
for i in range (n - 1 , 0 , - 1 ):
sum_i = 0
sum_n = 0
temp_i = i
temp_n = n
while temp_i > 0 :
sum_i + = temp_i % 10
temp_i / / = 10
while temp_n > 0 :
sum_n + = temp_n % 10
temp_n / / = 10
if sum_i > sum_n:
return i
return - 1
n = 824
print (findNumber(n))
|
C#
using System;
public class Program
{
public static int FindNumber( int n)
{
for ( int i = n - 1; i > 0; i--) {
int sum_i = 0, sum_n = 0;
int temp_i = i, temp_n = n;
while (temp_i > 0) {
sum_i += temp_i % 10;
temp_i /= 10;
}
while (temp_n > 0) {
sum_n += temp_n % 10;
temp_n /= 10;
}
if (sum_i > sum_n) {
return i;
}
}
return -1;
}
public static void Main()
{
int n = 824;
Console.WriteLine(FindNumber(n));
}
}
|
Javascript
function FindNumber(n) {
for (let i = n - 1; i > 0; i--) {
let sum_i = 0, sum_n = 0;
let temp_i = i, temp_n = n;
while (temp_i > 0) {
sum_i += temp_i % 10;
temp_i = Math.floor(temp_i / 10);
}
while (temp_n > 0) {
sum_n += temp_n % 10;
temp_n = Math.floor(temp_n / 10);
}
if (sum_i > sum_n) {
return i;
}
}
return -1;
}
function Main() {
let n = 824;
console.log(FindNumber(n));
}
Main();
|
Time Complexity: O(N^2)
Auxiliary Space: O(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++
#include <iostream>
using namespace std;
int sumOfDigits( int n)
{
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
int findNumber( int n)
{
int i = n - 1;
while (i > 0) {
if (sumOfDigits(i) > sumOfDigits(n))
return i;
i--;
}
return -1;
}
int main()
{
int n = 824;
cout << findNumber(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumOfDigits( int n)
{
int res = 0 ;
while (n > 0 ) {
res += n % 10 ;
n /= 10 ;
}
return res;
}
static int findNumber( int n)
{
int i = n - 1 ;
while (i > 0 ) {
if (sumOfDigits(i) > sumOfDigits(n))
return i;
i--;
}
return - 1 ;
}
public static void main (String[] args) {
int n = 824 ;
System.out.println (findNumber(n));
}
}
|
Python3
def sumOfDigits(n) :
res = 0 ;
while (n > 0 ) :
res + = n % 10
n / = 10
return res;
def findNumber(n) :
i = n - 1 ;
while (i > 0 ) :
if (sumOfDigits(i) > sumOfDigits(n)) :
return i
i - = 1
return - 1 ;
if __name__ = = "__main__" :
n = 824 ;
print (findNumber(n))
|
C#
using System;
class GFG
{
static int sumOfDigits( int n)
{
int res = 0;
while (n > 0)
{
res += n % 10;
n /= 10;
}
return res;
}
static int findNumber( int n)
{
int i = n - 1;
while (i > 0)
{
if (sumOfDigits(i) > sumOfDigits(n))
return i;
i--;
}
return -1;
}
static public void Main ()
{
int n = 824;
Console.WriteLine (findNumber(n));
}
}
|
PHP
<?php
function sumOfDigits( $n )
{
$res = 0;
while ( $n > 0)
{
$res += $n % 10;
$n /= 10;
}
return $res ;
}
function findNumber( $n )
{
$i = $n - 1;
while ( $i > 0)
{
if (sumOfDigits( $i ) > sumOfDigits( $n ))
return $i ;
$i --;
}
return -1;
}
$n = 824;
echo findNumber( $n );
?>
|
Javascript
<script>
function sumOfDigits(n)
{
var res = 0;
while (n > 0)
{
res += n % 10;
n = parseInt(n/10);
}
return res;
}
function findNumber(n)
{
var i = n - 1;
while (i > 0)
{
if (sumOfDigits(i) > sumOfDigits(n))
return i;
i--;
}
return -1;
}
var n = 824;
document.write(findNumber(n));
</script>
|
Time Complexity: O(N * log10N)
Auxiliary Space: O(1)
Last Updated :
09 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...