Check if a number S can be made divisible by D by repeatedly adding the remainder to S
Last Updated :
30 Jun, 2021
Given two integers S and D, the task is to check if the integer S can be made divisible by D or not by repeatedly adding S modulo D to S. If S is divisible by D, then print “Yes”. Otherwise, print “No”.
Examples:
Input: S = 3, D = 6
Output: Yes
Explanation:
Lets say that the value on the ith interval mod D is V(i) i.e., V(i) = (V(i – 1) + V(i – 1) % D) % D then,
V(0) = S % D = 3
V(1) = (V(0) + V(0) % D) % D = (3 + (3%6)) % 6 = 0
So, 6 is divisible by 6. Therefore, print “Yes”.
Input: S = 1, D = 5
Output: No
Approach: The given problem can be solved by using Pigeon Hole Principle. Follow the below steps to solve the problem:
- According to the principle, if there are more than D numbers that are taken modulo with D, then at least one value in the range ([0, D – 1]) will occur twice.
- Iterate for (D + 1) times and store the value of V(i) in a HashMap, and if there is a repetition then break out of the loop.
- There is an edge case when the remainder value is 0, exit out of the loop in that case as well as this is a case of divisibility, but our logic treats it as a cycle.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string isDivisibleByDivisor( int S, int D)
{
S %= D;
unordered_set< int > hashMap;
hashMap.insert(S);
for ( int i = 0; i <= D; i++) {
S += (S % D);
S %= D;
if (hashMap.find(S)
!= hashMap.end()) {
if (S == 0) {
return "Yes" ;
}
return "No" ;
}
else
hashMap.insert(S);
}
return "Yes" ;
}
int main()
{
int S = 3, D = 6;
cout << isDivisibleByDivisor(S, D);
return 0;
}
|
Java
import java.lang.*;
import java.util.*;
class GFG{
static String isDivisibleByDivisor( int S, int D)
{
S %= D;
Set<Integer> hashMap = new HashSet<>();
hashMap.add(S);
for ( int i = 0 ; i <= D; i++)
{
S += (S % D);
S %= D;
if (hashMap.contains(S))
{
if (S == 0 )
{
return "Yes" ;
}
return "No" ;
}
else
hashMap.add(S);
}
return "Yes" ;
}
public static void main(String[] args)
{
int S = 3 , D = 6 ;
System.out.println(isDivisibleByDivisor(S, D));
}
}
|
Python3
def isDivisibleByDivisor(S, D):
S % = D
hashMap = set ()
hashMap.add(S)
for i in range (D + 1 ):
S + = (S % D)
S % = D
if (S in hashMap):
if (S = = 0 ):
return "Yes"
return "No"
else :
hashMap.add(S)
return "Yes"
if __name__ = = '__main__' :
S = 3
D = 6
print (isDivisibleByDivisor(S, D))
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG {
static string isDivisibleByDivisor( int S, int D)
{
S %= D;
List< int > hashMap = new List< int >();;
hashMap.Add(S);
for ( int i = 0; i <= D; i++) {
S += (S % D);
S %= D;
if (hashMap.Contains(S)) {
if (S == 0) {
return "Yes" ;
}
return "No" ;
}
else
hashMap.Add(S);
}
return "Yes" ;
}
static void Main()
{
int S = 3, D = 6;
Console.Write( isDivisibleByDivisor(S, D));
}
}
|
Javascript
<script>
function isDivisibleByDivisor(S, D)
{
S %= D;
var hashMap = [];
hashMap.push(S);
for ( var i = 0; i <= D; i++)
{
S += S % D;
S %= D;
if (hashMap.includes(S)) {
if (S == 0) {
return "Yes" ;
}
return "No" ;
}
else hashMap.push(S);
}
return "Yes" ;
}
var S = 3,
D = 6;
document.write(isDivisibleByDivisor(S, D));
</script>
|
Time Complexity: O(D)
Auxiliary Space: O(D)
Share your thoughts in the comments
Please Login to comment...