Given three integers, the lower range L, the upper range U, and a digit M. The task is to count all the numbers between L and U such that the number is divisible by M and, also, it does not contain the digit M.
Examples:
Input: M = 9 ,L = 16 , U = 26
Output: 1
Explanation:
Within this given range ,the number that
follows the above two given conditions is: 18.
Input: M = 6 ,L = 88 , U = 102
Output: 2
Explanation:
Within this given range ,the numbers that
follows the above two given conditions are: 90 and 102.
Approach:
- The idea is to iterate from the lower range(L) to the upper range(U) and for each number,
- We will store the distinct digits of the number in a num variable and will check if the set contains the digit M or not as per the given conditions. If the number does not contain the given digit M and is divisible by M, then the counter is incremented by 1.
C++
#include<bits/stdc++.h>
using namespace std;
void contain( int L, int U, int M)
{
int count = 0;
for ( int j = L; j < U; j++)
{
set<string> num;
string str = to_string(j);
num.insert(str);
if (j % M == 0 and
num.find(to_string(M)) == num.end())
{
count += 1;
}
}
cout << count - 2;
}
int main()
{
int L = 106;
int U = 200;
int M = 7;
contain(L, U, M);
}
|
Java
import java.util.*;
class GFG{
static void contain( int L, int U, int M)
{
int count = 0 ;
for ( int j = L; j < U; j++)
{
HashSet<String> num = new HashSet<>();
String str = Integer.toString(j);
num.add(str);
if (j % M == 0 && !num.contains(
Integer.toString(M)))
{
count += 1 ;
}
}
System.out.println(count - 2 );
}
public static void main(String[] args)
{
int L = 106 ;
int U = 200 ;
int M = 7 ;
contain(L, U, M);
}
}
|
Python3
def contain (L,U,M):
count = 0
for j in range (L,U + 1 ):
num = set ( str (j))
if (j % M = = 0 and str (M) not in num):
count + = 1
print (count)
if __name__ = = '__main__' :
L = 106
U = 200
M = 7
contain(L,U,M)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void contain( int L, int U, int M)
{
int count = 0;
for ( int j = L; j < U; j++)
{
HashSet< string > num = new HashSet< string >();
string str = j.ToString();
num.Add(str);
if (j % M == 0 && !num.Contains(M.ToString()))
{
count += 1;
}
}
Console.Write(count - 2);
}
public static void Main( string [] args)
{
int L = 106;
int U = 200;
int M = 7;
contain(L, U, M);
}
}
|
Javascript
<script>
function contain(L, U, M)
{
let count = 0;
for (let j = L; j < U; j++)
{
let num = new Set();
let str = String(j);
num.add(str);
if (j % M == 0 && !num.has(String(M)))
{
count += 1;
}
}
document.write(count - 2);
}
let L = 106;
let U = 200;
let M = 7;
contain(L, U, M);
</script>
|
Time Complexity: O(U)
Auxiliary Space: O(U)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Nov, 2021
Like Article
Save Article