Imprima dígitos para cada elemento de array que no divide ningún dígito de ese elemento

Dada una array arr[] que consiste en N enteros positivos, la tarea para cada elemento de la array arr[i] es encontrar todos los dígitos de [0, 9] que no dividen ningún dígito presente en arr[i] .

Ejemplos:

Entrada: arr[] = {4162, 1152, 99842}
Salida:
4162 -> 5 7 8 9
1152 -> 3 4 6 7 8 9
99842 -> 5 6 7
Explicación:
Para arr[0] ( = 4162): Ninguno de los dígitos del elemento 4162 son divisibles por 5, 7, 8, 9.
Para arr[1]( = 1152): Ninguno de los dígitos del elemento 1152 son divisibles por 9, 8, 7, 6, 4, 3 Para arr [
2]( = 99842): Ninguno de los dígitos del elemento 99842 es divisible por 7, 6, 5.

Entrada: arr[] = {2021}
Salida:
2021 -> 3 4 5 6 7 8 9

 

Enfoque: siga los pasos a continuación para resolver el problema:

  • Recorra la array dada arr[] y realice los siguientes pasos:
    • Itere sobre el rango [2, 9] usando la variable i , y si no existe ningún dígito en el elemento arr[i] que sea divisible por i , imprima el dígito i .
    • De lo contrario, continúe para la siguiente iteración.

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 digits for each array
// element that doesn't divide any digit
// of the that element
void indivisibleDigits(int arr[], int N)
{
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        int num = 0;
 
        cout << arr[i] << ": ";
 
        // Iterate over the range [2, 9]
        for (int j = 2; j < 10; j++) {
 
            int temp = arr[i];
 
            // Stores if there exists any digit
            // in arr[i] which is divisible by j
            bool flag = true;
 
            while (temp > 0) {
 
                // If any digit of the number
                // is divisible by j
                if ((temp % 10) != 0
                    && (temp % 10) % j == 0) {
 
                    flag = false;
                    break;
                }
 
                temp /= 10;
            }
 
            // If the digit j doesn't
            // divide any digit of arr[i]
            if (flag) {
                cout << j << ' ';
            }
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4162, 1152, 99842 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    indivisibleDigits(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find digits for each array
  // element that doesn't divide any digit
  // of the that element
  static void indivisibleDigits(int[] arr, int N)
  {
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
      System.out.print(arr[i] + ": ");
 
      // Iterate over the range [2, 9]
      for (int j = 2; j < 10; j++)
      {
        int temp = arr[i];
 
        // Stores if there exists any digit
        // in arr[i] which is divisible by j
        boolean flag = true;
        while (temp > 0) {
 
          // If any digit of the number
          // is divisible by j
          if ((temp % 10) != 0
              && (temp % 10) % j == 0) {
 
            flag = false;
            break;
          }
 
          temp /= 10;
        }
 
        // If the digit j doesn't
        // divide any digit of arr[i]
        if (flag) {
          System.out.print(j + " ");
        }
      }
 
      System.out.println();
    }
  }
 
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 4162, 1152, 99842 };
    int N = arr.length;
 
    indivisibleDigits(arr, N);
  }
}
 
// This code is contributed by sanjoy_62.

Python3

# Python3 program for the above approach
 
# Function to find digits for each array
# element that doesn't divide any digit
# of the that element
def indivisibleDigits(arr, N) :
     
    # Traverse the array arr[]
    for i in range(N):
 
        num = 0
 
        print(arr[i], end = ' ')
 
        # Iterate over the range [2, 9]
        for j in range(2, 10):
 
            temp = arr[i]
 
            # Stores if there exists any digit
            # in arr[i] which is divisible by j
            flag = True
 
            while (temp > 0) :
 
                # If any digit of the number
                # is divisible by j
                if ((temp % 10) != 0
                    and (temp % 10) % j == 0) :
 
                    flag = False
                    break
                 
 
                temp //= 10
             
 
            # If the digit j doesn't
            # divide any digit of arr[i]
            if (flag) :
                print(j, end = ' ')
 
        print()
     
# Driver Code
 
arr = [ 4162, 1152, 99842 ]
N = len(arr)
 
indivisibleDigits(arr, N)
 
# This code is contributed by susmitakundugoaldanga.

C#

// C# program for the above approach
using System;
class GFG
{
 
  // Function to find digits for each array
  // element that doesn't divide any digit
  // of the that element
  static void indivisibleDigits(int[] arr, int N)
  {
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
      Console.Write(arr[i] + ": ");
 
      // Iterate over the range [2, 9]
      for (int j = 2; j < 10; j++)
      {
        int temp = arr[i];
 
        // Stores if there exists any digit
        // in arr[i] which is divisible by j
        bool flag = true;
        while (temp > 0) {
 
          // If any digit of the number
          // is divisible by j
          if ((temp % 10) != 0
              && (temp % 10) % j == 0) {
 
            flag = false;
            break;
          }
 
          temp /= 10;
        }
 
        // If the digit j doesn't
        // divide any digit of arr[i]
        if (flag) {
          Console.Write(j + " ");
        }
      }
 
      Console.WriteLine();
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 4162, 1152, 99842 };
    int N = arr.Length;
 
    indivisibleDigits(arr, N);
  }
}
 
// This code is contributed by rishavmahato348.

Javascript

<script>
 
// javascript program for the above approach
// Function to find digits for each array
    // element that doesn't divide any digit
    // of the that element
    function indivisibleDigits(arr , N) {
 
        // Traverse the array arr
        for (i = 0; i < N; i++) {
            document.write(arr[i] + ": ");
 
            // Iterate over the range [2, 9]
            for (j = 2; j < 10; j++) {
                var temp = arr[i];
 
                // Stores if there exists any digit
                // in arr[i] which is divisible by j
                var flag = true;
                while (temp > 0) {
 
                    // If any digit of the number
                    // is divisible by j
                    if ((temp % 10) != 0 && (temp % 10) % j == 0) {
 
                        flag = false;
                        break;
                    }
 
                    temp = parseInt(temp/10);
                }
 
                // If the digit j doesn't
                // divide any digit of arr[i]
                if (flag) {
                    document.write(j + " ");
                }
            }
 
            document.write("<br/>");
        }
    }
 
    // Driver Code
     
        var arr = [ 4162, 1152, 99842 ];
        var N = arr.length;
 
        indivisibleDigits(arr, N);
 
// This code contributed by aashish1995
</script>
Producción: 

4162: 5 7 8 9 
1152: 3 4 6 7 8 9 
99842: 5 6 7

 

Complejidad de tiempo: O(10*N*log 10 N)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

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