El número más pequeño que se requiere agregar a M para hacerlo divisible por N

Dados dos enteros positivos M y N , la tarea es calcular el número más pequeño que se debe sumar a M , para hacerlo divisible por N .

Ejemplos:

Entrada: M = 6, N = 7
Salida: 1
Explicación: 1 es el número más pequeño que se puede sumar a 6 para hacerlo divisible por 7.

Entrada: M = 100, N = 28
Salida: 12

Enfoque: La idea es encontrar el número más pequeño mayor o igual a M , que sea divisible por N , y luego restarle M. Para obtener el múltiplo más pequeño de N ≥ M , divida M + N entre N . Si el resto es 0 , entonces el valor es M. De lo contrario, el valor es M + N – resto .

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
int findNum(int N, int K)
{
    int rem = (N + K) % K;
 
    if (rem == 0)
        return N;
    else
        return N + K - rem;
}
 
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
int findSmallest(int M, int N)
{
    // Stores the smallest multiple
    // of N, greater than or equal to M
    int x = findNum(M, N);
 
    // Return the result
    return x - M;
}
 
// Driver Code
int main()
{
    // Given Input
    int M = 100, N = 28;
 
    // Function Call
    cout << findSmallest(M, N);
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
public static int findNum(int N, int K)
{
    int rem = (N + K) % K;
 
    if (rem == 0)
        return N;
    else
        return N + K - rem;
}
 
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
public static int findSmallest(int M, int N)
{
     
    // Stores the smallest multiple
    // of N, greater than or equal to M
    int x = findNum(M, N);
 
    // Return the result
    return x - M;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given Input
    int M = 100, N = 28;
 
    // Function Call
    System.out.println(findSmallest(M, N));
}}
 
// This code is contributed by SoumikMondal

Python3

# Python3 program for the above approach
 
# Function to find the smallest
# number greater than or equal
# to N, that is divisible by k
def findNum(N, K):
     
    rem = (N + K) % K
 
    if (rem == 0):
        return N
    else:
        return N + K - rem
 
# Function to find the smallest
# number required to be added to
# to M to make it divisible by N
def findSmallest(M, N):
     
    # Stores the smallest multiple
    # of N, greater than or equal to M
    x = findNum(M, N)
 
    # Return the result
    return x - M
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    M = 100
    N = 28
 
    # Function Call
    print(findSmallest(M, N))
     
# This code is contributed by ipg2016107

C#

// C# program for the above approach
using System;
         
class GFG{
     
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
public static int findNum(int N, int K)
{
    int rem = (N + K) % K;
  
    if (rem == 0)
        return N;
    else
        return N + K - rem;
}
  
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
public static int findSmallest(int M, int N)
{
     
    // Stores the smallest multiple
    // of N, greater than or equal to M
    int x = findNum(M, N);
  
    // Return the result
    return x - M;
}
     
// Driver Code
public static void Main()
{
     
    // Given Input
    int M = 100, N = 28;
  
    // Function Call
    Console.WriteLine(findSmallest(M, N));
}
}
 
// This code is contributed by susmitakundugoaldanga

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to find the smallest
// number greater than or equal
// to N, that is divisible by k
function findNum(N, K)
{
    var rem = (N + K) % K;
 
    if (rem == 0)
        return N;
    else
        return N + K - rem;
}
 
// Function to find the smallest
// number required to be added to
// to M to make it divisible by N
function findSmallest(M, N)
{
     
    // Stores the smallest multiple
    // of N, greater than or equal to M
    var x = findNum(M, N);
 
    // Return the result
    return x - M;
}
 
// Driver Code
 
// Given Input
var M = 100, N = 28;
 
// Function Call
document.write(findSmallest(M, N));
 
// This code is contributed by itsok
 
</script>
Producción: 

12

 

Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por pawanharwani11 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 *