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 does 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 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; } |
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.

