Skip to content
Related Articles

Related Articles

Improve Article

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if S is divisible
// by D while changing S to (S + S%D)
string isDivisibleByDivisor(int S, int D)
{
    // V(0) = S % D
    S %= D;
 
    // Stores the encountered values
    unordered_set<int> hashMap;
    hashMap.insert(S);
 
    for (int i = 0; i <= D; i++) {
 
        // V(i) = (V(i-1) + V(i-1)%D) % D
        S += (S % D);
        S %= D;
 
        // Check if the value has
        // already been encountered
        if (hashMap.find(S)
            != hashMap.end()) {
 
            // Edge Case
            if (S == 0) {
                return "Yes";
            }
            return "No";
        }
 
        // Otherwise, insert it into
        // the hashmap
        else
            hashMap.insert(S);
    }
    return "Yes";
}
 
// Driver Code
int main()
{
    int S = 3, D = 6;
    cout << isDivisibleByDivisor(S, D);
 
    return 0;
}

Java




// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
     
// Function to check if S is divisible
// by D while changing S to (S + S%D)
static String isDivisibleByDivisor(int S, int D)
{
     
    // V(0) = S % D
    S %= D;
 
    // Stores the encountered values
    Set<Integer> hashMap = new HashSet<>();
    hashMap.add(S);
 
    for(int i = 0; i <= D; i++)
    {
         
        // V(i) = (V(i-1) + V(i-1)%D) % D
        S += (S % D);
        S %= D;
 
        // Check if the value has
        // already been encountered
        if (hashMap.contains(S))
        {
             
            // Edge Case
            if (S == 0)
            {
                return "Yes";
            }
            return "No";
        }
 
        // Otherwise, insert it into
        // the hashmap
        else
            hashMap.add(S);
    }
    return "Yes";
}
 
// Driver code
public static void main(String[] args)
{
    int S = 3, D = 6;
     
    System.out.println(isDivisibleByDivisor(S, D));
}
}
 
// This code is contributed by offbeat

Python3




# Python 3 program for the above approach
 
# Function to check if S is divisible
# by D while changing S to (S + S%D)
def isDivisibleByDivisor(S, D):
    # V(0) = S % D
    S %= D
 
    # Stores the encountered values
    hashMap = set()
    hashMap.add(S)
 
    for i in range(D+1):
       
        # V(i) = (V(i-1) + V(i-1)%D) % D
        S += (S % D)
        S %= D
 
        # Check if the value has
        # already been encountered
        if (S in hashMap):
           
            # Edge Case
            if (S == 0):
                return "Yes"
            return "No"
 
        # Otherwise, insert it into
        # the hashmap
        else:
            hashMap.add(S)
    return "Yes"
 
# Driver Code
if __name__ == '__main__':
    S = 3
    D = 6
    print(isDivisibleByDivisor(S, D))
 
    # This code is contributed by bgangwar59.

C#




// C# program for the above approach
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG {
     
// Function to check if S is divisible
// by D while changing S to (S + S%D)
static string isDivisibleByDivisor(int S, int D)
{
    // V(0) = S % D
    S %= D;
   
    // Stores the encountered values
    List<int> hashMap = new List<int>();;
    hashMap.Add(S);
   
    for (int i = 0; i <= D; i++) {
   
        // V(i) = (V(i-1) + V(i-1)%D) % D
        S += (S % D);
        S %= D;
   
        // Check if the value has
        // already been encountered
        if (hashMap.Contains(S)) {
   
            // Edge Case
            if (S == 0) {
                return "Yes";
            }
            return "No";
        }
   
        // Otherwise, insert it into
        // the hashmap
        else
            hashMap.Add(S);
    }
    return "Yes";
}
 
    // Driver Code
    static void Main()
    {
        int S = 3, D = 6;
        Console.Write( isDivisibleByDivisor(S, D));
    }
}
 
// This code is contributed by sanjoy_62.

Javascript




<script>
      // JavaScript program for the above approach
      // Function to check if S is divisible
      // by D while changing S to (S + S%D)
      function isDivisibleByDivisor(S, D)
      {
       
        // V(0) = S % D
        S %= D;
 
        // Stores the encountered values
        var hashMap = [];
        hashMap.push(S);
 
        for (var i = 0; i <= D; i++)
        {
         
          // V(i) = (V(i-1) + V(i-1)%D) % D
          S += S % D;
          S %= D;
 
          // Check if the value has
          // already been encountered
          if (hashMap.includes(S)) {
            // Edge Case
            if (S == 0) {
              return "Yes";
            }
            return "No";
          }
 
          // Otherwise, insert it into
          // the hashmap
          else hashMap.push(S);
        }
        return "Yes";
      }
 
      // Driver Code
      var S = 3,
        D = 6;
      document.write(isDivisibleByDivisor(S, D));
       
      // This code is contributed by rdtank.
    </script>
Output: 
Yes

 

Time Complexity: O(D)
Auxiliary Space: O(D)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :