Print digits for each array element that does not divide any digit of that element
Last Updated :
29 Apr, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void indivisibleDigits( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int num = 0;
cout << arr[i] << ": " ;
for ( int j = 2; j < 10; j++) {
int temp = arr[i];
bool flag = true ;
while (temp > 0) {
if ((temp % 10) != 0
&& (temp % 10) % j == 0) {
flag = false ;
break ;
}
temp /= 10;
}
if (flag) {
cout << j << ' ' ;
}
}
cout << endl;
}
}
int main()
{
int arr[] = { 4162, 1152, 99842 };
int N = sizeof (arr) / sizeof (arr[0]);
indivisibleDigits(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static void indivisibleDigits( int [] arr, int N)
{
for ( int i = 0 ; i < N; i++)
{
System.out.print(arr[i] + ": " );
for ( int j = 2 ; j < 10 ; j++)
{
int temp = arr[i];
boolean flag = true ;
while (temp > 0 ) {
if ((temp % 10 ) != 0
&& (temp % 10 ) % j == 0 ) {
flag = false ;
break ;
}
temp /= 10 ;
}
if (flag) {
System.out.print(j + " " );
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int [] arr = { 4162 , 1152 , 99842 };
int N = arr.length;
indivisibleDigits(arr, N);
}
}
|
Python3
def indivisibleDigits(arr, N) :
for i in range (N):
num = 0
print (arr[i], end = ' ' )
for j in range ( 2 , 10 ):
temp = arr[i]
flag = True
while (temp > 0 ) :
if ((temp % 10 ) ! = 0
and (temp % 10 ) % j = = 0 ) :
flag = False
break
temp / / = 10
if (flag) :
print (j, end = ' ' )
print ()
arr = [ 4162 , 1152 , 99842 ]
N = len (arr)
indivisibleDigits(arr, N)
|
C#
using System;
class GFG
{
static void indivisibleDigits( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + ": " );
for ( int j = 2; j < 10; j++)
{
int temp = arr[i];
bool flag = true ;
while (temp > 0) {
if ((temp % 10) != 0
&& (temp % 10) % j == 0) {
flag = false ;
break ;
}
temp /= 10;
}
if (flag) {
Console.Write(j + " " );
}
}
Console.WriteLine();
}
}
public static void Main()
{
int [] arr = { 4162, 1152, 99842 };
int N = arr.Length;
indivisibleDigits(arr, N);
}
}
|
Javascript
<script>
function indivisibleDigits(arr , N) {
for (i = 0; i < N; i++) {
document.write(arr[i] + ": " );
for (j = 2; j < 10; j++) {
var temp = arr[i];
var flag = true ;
while (temp > 0) {
if ((temp % 10) != 0 && (temp % 10) % j == 0) {
flag = false ;
break ;
}
temp = parseInt(temp/10);
}
if (flag) {
document.write(j + " " );
}
}
document.write( "<br/>" );
}
}
var arr = [ 4162, 1152, 99842 ];
var N = arr.length;
indivisibleDigits(arr, N);
</script>
|
Output:
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...