El múltiplo más pequeño de 3 que consta de tres dígitos distintos de cero dados

Dados tres dígitos distintos de cero 0 < A, B, C < 9 . La tarea es encontrar el número más pequeño divisible por 3 cuyos dígitos estén todos en el conjunto {A, B, C}. 
Nota: No es necesario incluir los tres dígitos. El resultado puede ser A , AA , AB , CCA etc.
Ejemplos: 
 

Entrada: A = 2, B = 4, C = 7 
Salida: 24 
24 es el número mínimo divisible por 3 que se puede formar con los dígitos dados.
Entrada: A = 1, B = 2, C = 3 
Salida:
 

Enfoque: tome los tres números en una array y ordénelos en orden creciente. Ahora verifica si algún número es divisible por 3 , si es así , entonces el número será la respuesta. 
Si no es así, vuelve a comprobarlo tomando cualquiera de los dos números. Finalmente tome el número más pequeño y nuestro resultado es este número repetido tres veces.
¿Por qué no podemos obtener una respuesta de más de tres dígitos?  
Incluso si cualquier número no es divisible por 3, repetir el número más pequeño 3 veces lo hará divisible por 3. Tenga en cuenta que un número es divisible por 3 si la suma de sus dígitos es divisible por 3 .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the minimum number
// divisible by 3 formed by the given digits
int printSmallest(int a[3])
{
    int sum, sum1;
 
    // Sort the given array in ascending
    sort(a, a + 3);
 
    int i, j, k, num;
 
    // Check if any single digit is divisible by 3
    for (int i = 0; i < 3; i++) {
        if (a[i] % 3 == 0)
            return a[i];
    }
 
    // Check if any two digit number formed by
    // the given digits is divisible by 3
    // starting from the minimum
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
 
            // Generate the two digit number
            num = (a[i] * 10) + a[j];
            if (num % 3 == 0)
                return num;
        }
    }
 
    // If none of the above is true, we can
    // form three digit number by taking a[0]
    // three times.
    return a[0]*100 + a[0]*10 + a[0];
}
 
// Driver code
int main()
{
    int arr[] = { 7, 7, 1 };
    cout << printSmallest(arr);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.Arrays;
public class GFG {
 
// Function to return the minimum number
// divisible by 3 formed by the given digits
    static int printSmallest(int a[]) {
        int sum, sum1;
 
        // Sort the given array in ascending
        Arrays.sort(a);
 
        int i, j, k, num;
 
        // Check if any single digit is divisible by 3
        for (i = 0; i < 3; i++) {
            if (a[i] % 3 == 0) {
                return a[i];
            }
        }
 
        // Check if any two digit number formed by
        // the given digits is divisible by 3
        // starting from the minimum
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
 
                // Generate the two digit number
                num = (a[i] * 10) + a[j];
                if (num % 3 == 0) {
                    return num;
                }
            }
        }
 
        // If none of the above is true, we can
        // form three digit number by taking a[0]
        // three times.
        return a[0] * 100 + a[0] * 10 + a[0];
    }
 
// Driver code
    public static void main(String[] args) {
 
        int arr[] = {7, 7, 1};
        System.out.println(printSmallest(arr));
 
    }
}
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of the approach
 
# Function to return the minimum
# number divisible by 3 formed by
# the given digits
def printSmallest(a, n):
 
    sum0, sum1 = 0, 0
 
    # Sort the given array in ascending
    a = sorted(a)
 
    # Check if any single digit is
    # divisible by 3
    for i in range(n):
        if (a[i] % 3 == 0):
            return a[i]
 
    # Check if any two digit number
    # formed by the given digits is
    # divisible by 3 starting from
    # the minimum
    for i in range(n):
        for j in range(n):
             
            # Generate the two digit number
            num = (a[i] * 10) + a[j]
            if (num % 3 == 0):
                return num
 
    # If none of the above is true, we can
    # form three digit number by taking a[0]
    # three times.
    return a[0] * 100 + a[0] * 10 + a[0]
 
# Driver code
arr = [7, 7, 1 ]
n = len(arr)
 
print(printSmallest(arr, n))
 
# This code is contributed
# by mohit kumar 29

C#

     
// C# implementation of the approach
using System;
 
public class GFG {
  
// Function to return the minimum number
// divisible by 3 formed by the given digits
    static int printSmallest(int []a) {
  
        // Sort the given array in ascending
        Array.Sort(a);
  
        int i, j, num;
  
        // Check if any single digit is divisible by 3
        for (i = 0; i < 3; i++) {
            if (a[i] % 3 == 0) {
                return a[i];
            }
        }
  
        // Check if any two digit number formed by
        // the given digits is divisible by 3
        // starting from the minimum
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
  
                // Generate the two digit number
                num = (a[i] * 10) + a[j];
                if (num % 3 == 0) {
                    return num;
                }
            }
        }
  
        // If none of the above is true, we can
        // form three digit number by taking a[0]
        // three times.
        return a[0] * 100 + a[0] * 10 + a[0];
    }
  
// Driver code
    public static void Main() {
  
        int []arr = {7, 7, 1};
        Console.Write(printSmallest(arr));
  
    }
}
//This code is contributed by Rajput-Ji

PHP

<?php
// PHP implementation of the approach
 
// Function to return the minimum number
// divisible by 3 formed by the given digits
function printSmallest($a)
{
 
    // Sort the given array in ascending
    sort($a);
 
    // Check if any single digit
    // is divisible by 3
    for ($i = 0; $i < 3; $i++)
    {
        if ($a[$i] % 3 == 0)
            return $a[$i];
    }
 
    // Check if any two digit number formed
    // by the given digits is divisible by 3
    // starting from the minimum
    for ($i = 0; $i < 3; $i++)
    {
        for ($j = 0; $j < 3; $j++)
        {
 
            // Generate the two digit number
            $num = ($a[$i] * 10) + $a[$j];
            if ($num % 3 == 0)
                return $num;
        }
    }
 
    // If none of the above is true, we can
    // form three digit number by taking a[0]
    // three times.
    return $a[0] * 100 + $a[0] * 10 + $a[0];
}
 
// Driver code
$arr = array( 7, 7, 1 );
echo printSmallest($arr);
 
// This code is contributed by Ryuga
?>

Javascript

<script>
 
    // JavaScript implementation of the approach
     
    // Function to return the minimum number
    // divisible by 3 formed by the given digits
    function printSmallest(a) {
    
        // Sort the given array in ascending
        a.sort(function(a, b){return a - b});
    
        let i, j, num;
    
        // Check if any single digit is divisible by 3
        for (i = 0; i < 3; i++) {
            if (a[i] % 3 == 0) {
                return a[i];
            }
        }
    
        // Check if any two digit number formed by
        // the given digits is divisible by 3
        // starting from the minimum
        for (i = 0; i < 3; i++) {
            for (j = 0; j < 3; j++) {
    
                // Generate the two digit number
                num = (a[i] * 10) + a[j];
                if (num % 3 == 0) {
                    return num;
                }
            }
        }
    
        // If none of the above is true, we can
        // form three digit number by taking a[0]
        // three times.
        return a[0] * 100 + a[0] * 10 + a[0];
    }
     
    let arr = [7, 7, 1];
      document.write(printSmallest(arr));
 
</script>
Producción: 

111

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Vivek.Pandit 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 *