Números de N dígitos divisibles por 5 formados a partir de los M dígitos

Dados M dígitos únicos y un número N. La tarea es encontrar la cantidad de números de N dígitos que se pueden formar a partir de los M dígitos dados, que son divisibles por 5 y ninguno de los dígitos se repite.

Nota : Si no es posible formar un número de N dígitos a partir de los dígitos dados, imprima -1.

Ejemplos: 

Input : N = 3, M = 6, digits[] = {2, 3, 5, 6, 7, 9} 
Output : 20
Input : N = 5, M = 6, digits[] = {0, 3, 5, 6, 7, 9} 
Output : 240

Para que un número sea divisible por 5 , la única condición es que el dígito en el lugar de la unidad en el número sea 0 o 5 .
Entonces, para encontrar el conteo de números que son divisibles por 5 y se pueden formar a partir de los dígitos dados, haz lo siguiente:  

  • Compruebe si los dígitos dados contienen tanto 0 como 5.
  • Si los dígitos dados contienen tanto 0 como 5, entonces el lugar de la unidad se puede llenar de 2 maneras; de lo contrario, el lugar de la unidad se puede llenar de 1 manera.
  • Ahora, el lugar de las decenas ahora se puede llenar con cualquiera de los dígitos M-1 restantes. Entonces, hay (M-1) formas de llenar el lugar de las decenas.
  • De manera similar, el lugar de la centena ahora se puede llenar con cualquiera de los dígitos restantes (M-2) y así sucesivamente.

Por lo tanto, si los dígitos dados tienen tanto 0 como 5:  

Required number of numbers = 2 * (M-1)* (M-2)...N-times. 

De lo contrario, si los dígitos dados tienen uno de 0 y 5 y no ambos:  

Required number of numbers = 1 * (M-1)* (M-2)...N-times. 

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

C++

// CPP program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
int numbers(int n, int arr[], int m)
{
    int isZero = 0, isFive = 0;
    int result = 0;
 
    // If it is not possible to form
    // n digit number from the given
    // m digits without repetition
    if (m < n) {
        return -1;
    }
 
    for (int i = 0; i < m; i++) {
        if (arr[i] == 0)
            isZero = 1;
 
        if (arr[i] == 5)
            isFive = 1;
    }
 
    // If both zero and five exists
    if (isZero && isFive) {
        result = 2;
 
        // Remaining N-1 iterations
        for (int i = 0; i < n - 1; i++) {
            result = result * (--m);
        }
    }
    else if (isZero || isFive) {
        result = 1;
 
        // Remaining N-1 iterations
        for (int i = 0; i < n - 1; i++) {
            result = result * (--m);
        }
    }
    else
        result = -1;
 
    return result;
}
 
// Driver code
int main()
{
    int n = 3, m = 6;
 
    int arr[] = { 2, 3, 5, 6, 7, 9 };
 
    cout << numbers(n, arr, m);
 
    return 0;
}

Java

// Java program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
 
class GFG {
 
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
    static int numbers(int n, int arr[], int m) {
        int isZero = 0, isFive = 0;
        int result = 0;
 
        // If it is not possible to form
        // n digit number from the given
        // m digits without repetition
        if (m < n) {
            return -1;
        }
 
        for (int i = 0; i < m; i++) {
            if (arr[i] == 0) {
                isZero = 1;
            }
 
            if (arr[i] == 5) {
                isFive = 1;
            }
        }
 
        // If both zero and five exists
        if (isZero == 1 && isFive == 1) {
            result = 2;
 
            // Remaining N-1 iterations
            for (int i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else if (isZero == 1 || isFive == 1) {
            result = 1;
 
            // Remaining N-1 iterations
            for (int i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else {
            result = -1;
        }
 
        return result;
    }
 
// Driver code
    public static void main(String[] args) {
        int n = 3, m = 6;
 
        int arr[] = {2, 3, 5, 6, 7, 9};
        System.out.println(numbers(n, arr, m));
 
    }
}
// This code is contributed by RAJPUT-JI

Python 3

# Python 3 program to find the count
# of all possible N digit numbers which
# are divisible by 5 formed from M digits
 
# Function to find the count of all
# possible N digit numbers which are
# divisible by 5 formed from M digits
def numbers(n, arr, m):
 
    isZero = 0
    isFive = 0
    result = 0
 
    # If it is not possible to form
    # n digit number from the given
    # m digits without repetition
    if (m < n) :
        return -1
 
    for i in range(m) :
        if (arr[i] == 0):
            isZero = 1
 
        if (arr[i] == 5):
            isFive = 1
 
    # If both zero and five exists
    if (isZero and isFive) :
        result = 2
 
        # Remaining N-1 iterations
        for i in range( n - 1):
            m -= 1
            result = result * (m)
 
    elif (isZero or isFive) :
        result = 1
 
        # Remaining N-1 iterations
        for i in range(n - 1) :
            m -= 1
            result = result * (m)
    else:
        result = -1
 
    return result
 
# Driver code
if __name__ == "__main__":
    n = 3
    m = 6
 
    arr = [ 2, 3, 5, 6, 7, 9]
 
    print(numbers(n, arr, m))
 
# This code is contributed by ChitraNayal

C#

// C# program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
using System;
public class GFG {
 
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
    static int numbers(int n, int []arr, int m) {
        int isZero = 0, isFive = 0;
        int result = 0;
 
        // If it is not possible to form
        // n digit number from the given
        // m digits without repetition
        if (m < n) {
            return -1;
        }
 
        for (int i = 0; i < m; i++) {
            if (arr[i] == 0) {
                isZero = 1;
            }
 
            if (arr[i] == 5) {
                isFive = 1;
            }
        }
 
        // If both zero and five exists
        if (isZero == 1 && isFive == 1) {
            result = 2;
 
            // Remaining N-1 iterations
            for (int i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else if (isZero == 1 || isFive == 1) {
            result = 1;
 
            // Remaining N-1 iterations
            for (int i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else {
            result = -1;
        }
 
        return result;
    }
 
// Driver code
    public static void Main() {
        int n = 3, m = 6;
 
        int []arr = {2, 3, 5, 6, 7, 9};
        Console.WriteLine(numbers(n, arr, m));
 
    }
}
// This code is contributed by RAJPUT-JI

PHP

<?php
// PHP program to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
 
// Function to find the count of all
// possible N digit numbers which are
// divisible by 5 formed from M digits
function numbers($n, $arr, $m)
{
    $isZero = 0;
    $isFive = 0;
    $result = 0;
 
    // If it is not possible to form
    // n digit number from the given
    // m digits without repetition
    if ($m < $n)
    {
        return -1;
    }
 
    for ($i = 0; $i < $m; $i++)
    {
        if ($arr[$i] == 0)
            $isZero = 1;
 
        if ($arr[$i] == 5)
            $isFive = 1;
    }
 
    // If both zero and five exists
    if ($isZero && $isFive)
    {
        $result = 2;
 
        // Remaining N-1 iterations
        for ($i = 0; $i < $n - 1; $i++)
        {
            $result = $result * (--$m);
        }
    }
    else if ($isZero || $isFive)
    {
        $result = 1;
 
        // Remaining N-1 iterations
        for ($i = 0; $i < $n - 1; $i++)
        {
            $result = $result * (--$m);
        }
    }
    else
        $result = -1;
 
    return $result;
}
 
// Driver code
$n = 3;
$m = 6;
 
$arr = array( 2, 3, 5, 6, 7, 9 );
 
echo numbers($n, $arr, $m);
 
// This code is contributed by jit_t
?>

Javascript

<script>
    // Javascript program to find the count of all
    // possible N digit numbers which are
    // divisible by 5 formed from M digits
     
    // Function to find the count of all
    // possible N digit numbers which are
    // divisible by 5 formed from M digits
    function numbers(n, arr, m) {
        let isZero = 0, isFive = 0;
        let result = 0;
   
        // If it is not possible to form
        // n digit number from the given
        // m digits without repetition
        if (m < n) {
            return -1;
        }
   
        for (let i = 0; i < m; i++) {
            if (arr[i] == 0) {
                isZero = 1;
            }
   
            if (arr[i] == 5) {
                isFive = 1;
            }
        }
   
        // If both zero and five exists
        if (isZero == 1 && isFive == 1) {
            result = 2;
   
            // Remaining N-1 iterations
            for (let i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else if (isZero == 1 || isFive == 1) {
            result = 1;
   
            // Remaining N-1 iterations
            for (let i = 0; i < n - 1; i++) {
                result = result * (--m);
            }
        } else {
            result = -1;
        }
   
        return result;
    }
     
    let n = 3, m = 6;
   
    let arr = [2, 3, 5, 6, 7, 9];
    document.write(numbers(n, arr, m));
     
</script>
Producción: 

20

 

Complejidad de Tiempo: O(m + n), Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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