Número más grande menor que N con suma de dígitos mayor que la suma de dígitos de N

Dado un número entero N , la tarea es encontrar el mayor número menor que N tal que la suma de sus dígitos sea mayor que la suma de los dígitos de N . Si la condición no se cumple para ningún número, imprima -1 .
Ejemplos: 
 

Entrada: N = 100 
Salida: 99 
99 es el número más grande menor que 100 cuya suma de dígitos es mayor que la suma de los dígitos de 100
Entrada: N = 49 
Salida: -1 
 

Enfoque: Comience un ciclo de N-1 a 1 y verifique si la suma de los dígitos de cualquier número es mayor que la suma de los dígitos de N. El primer número que satisface la condición es el número requerido.
A continuación se muestra la implementación del enfoque anterior:
 

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;
}

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   
}

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

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

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
?>

Javascript

<script>
// javascript implementation of the approach
 
    // Function to return the sum of the digits of n
    function sumOfDigits(n)
    {
        var res = 0;
 
        // Loop for each digit of the number
        while (n > 0)
        {
            res += n % 10;
            n = parseInt(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
        var 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
    var n = 824;
    document.write(findNumber(n));
 
// This code is contributed by Princi Singh
</script>
Producción: 

819

 

Complejidad de tiempo: O(N * log 10 N)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Samdare B y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *