Given an array arr[] consisting of N positive integers, the task for each array element arr[i] is to find all the digits from [0, 9] that do not divide any digit present in arr[i].
Examples:
Input: arr[] = {4162, 1152, 99842}
Output:
4162 -> 5 7 8 9
1152 -> 3 4 6 7 8 9
99842 -> 5 6 7
Explanation:
For arr[0] ( = 4162): None of the digits of the element 4162 are divisible by 5, 7, 8, 9.
For arr[1]( = 1152): None of the digits of the element 1152 are divisible by 9, 8, 7, 6, 4, 3.
For arr[2]( = 99842): None of the digits of the element 99842 are divisible by 7, 6, 5.Input: arr[] = {2021}
Output:
2021 -> 3 4 5 6 7 8 9
Approach: Follow the steps below to solve the problem:
- Traverse the given array arr[] and perform the following steps:
- Iterate over the range [2, 9] using the variable i, and if there doesn’t exist any digit in the element arr[i] that is divisible by i, then print the digit i.
- Otherwise, continue for the next iteration.
Below is the implementation of the above approach:
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 elementvoid 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 Codeint main(){ int arr[] = { 4162, 1152, 99842 }; int N = sizeof(arr) / sizeof(arr[0]); indivisibleDigits(arr, N); return 0;} |
Java
// Java program for the above approachimport 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 elementdef 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 Codearr = [ 4162, 1152, 99842 ]N = len(arr)indivisibleDigits(arr, N)# This code is contributed by susmitakundugoaldanga. |
C#
// C# program for the above approachusing 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> |
4162: 5 7 8 9 1152: 3 4 6 7 8 9 99842: 5 6 7
Time Complexity: O(10*N*log10N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer DSA Live Classes


