Open In App

Find count of numbers from 0 to n which satisfies the given equation for a value K

Given three positive integers a, b and n, our task is to find the total count of all the numbers K ranging from 0 to n which satisfies the given equation (( k % a ) % b) = (( k % b ) % a).

Examples:

Input: a = 3, b = 4, n = 25 
Output: 10 
Explanation: 
The values which satisfies the above equation are 0 1 2 3 12 13 14 15 24 25. For example, for K = 13; ((13 % 3) % 4) gives 1 and ((13 % 4) % 3) also gives 1 as output. 
Input: a = 1, b = 13, n = 500 
Output: 501 
Explanation: 
In total there are 501 numbers between 0 and 500 which satisfies the given equation. 
 

Approach:
To solve the problem mentioned above we have the given condition (( k % a ) % b) = (( k % b ) % a) which will always be satisfied for numbers from 0 to max(a, b) – 1. So according to the statement provided above if we have a <= b then check all number from 0 to b-1 and we have the following two cases: 
 

Similarly, we can check the cases for a > b. So now we need to check all numbers which are divisible by both a and b in the range 0 to n. This can be found with the help of LCM of a and b. So, now we can easily find the number of multiples of the LCM in the range 0 to n by diving n by LCM. We will add 1 to the multiples to include 0 as a multiple. And then we have to multiply the number of multiples by max(a, b) so that we can find all numbers which satisfy the given condition. But if the sum of the last multiple and max(a, b) exceeds our range of n numbers then we need to exclude the extra numbers.
Below is the implementation of the above approach: 
 




// C++ implementation to Find the total
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the values
int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
 
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
 
    // Find the values which satisfies
    // the given condition
    int answer = max(a, b) * multiples;
 
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + max(a, b);
 
    if (lastvalue > n)
        answer = answer - (lastvalue - n - 1);
 
    // Return the final result
    return answer;
}
 
// Driver code
int main()
{
    int a = 1, b = 13, n = 500;
 
    cout << findAns(a, b, n) << endl;
}




// Java implementation to find the total
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
class GFG{
 
// Function to find the values
static int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
 
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
 
    // Find the values which satisfies
    // the given condition
    int answer = Math.max(a, b) * multiples;
 
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + Math.max(a, b);
    if (lastvalue > n)
        answer = answer - (lastvalue - n - 1);
 
    // Return the final result
    return answer;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver code
public static void main(String[] args)
{
    int a = 1, b = 13, n = 500;
    System.out.print(findAns(a, b, n) + "\n");
}
}
 
// This code is contributed by Amit Katiyar




# Python3 implementation to find the total
# count of all the numbers from 0 to n which
# satisfies the given equation for a value K
 
# Function to find the values
def findAns(a, b, n):
     
    # Calculate the LCM
    lcm = (a * b) // __gcd(a, b);
 
    # Calculate the multiples of lcm
    multiples = (n // lcm) + 1;
 
    # Find the values which satisfies
    # the given condition
    answer = max(a, b) * multiples;
 
    # Subtract the extra values
    lastvalue = lcm * (n // lcm) + max(a, b);
     
    if (lastvalue > n):
        answer = answer - (lastvalue - n - 1);
 
    # Return the final result
    return answer;
 
def __gcd(a, b):
     
    if(b == 0):
        return a;
    else:
        return __gcd(b, a % b);
         
# Driver code
if __name__ == '__main__':
     
    a = 1;
    b = 13;
    n = 500;
     
    print(findAns(a, b, n));
     
# This code is contributed by 29AjayKumar




// C# implementation to find the total
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K
using System;
 
class GFG{
 
// Function to find the values
static int findAns(int a, int b, int n)
{
    // Calculate the LCM
    int lcm = (a * b) / __gcd(a, b);
 
    // Calculate the multiples of lcm
    int multiples = (n / lcm) + 1;
 
    // Find the values which satisfies
    // the given condition
    int answer = Math.Max(a, b) * multiples;
 
    // Subtract the extra values
    int lastvalue = lcm * (n / lcm) + Math.Max(a, b);
    if (lastvalue > n)
    {
        answer = answer - (lastvalue - n - 1);
    }
 
    // Return the readonly result
    return answer;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver code
public static void Main(String[] args)
{
    int a = 1, b = 13, n = 500;
    Console.Write(findAns(a, b, n) + "\n");
}
}
 
// This code is contributed by sapnasingh4991




<script>
// javascript implementation to find the total
// count of all the numbers from 0 to n which
// satisfies the given equation for a value K   
// Function to find the values
    function findAns(a , b , n) {
        // Calculate the LCM
        var lcm = (a * b) / __gcd(a, b);
 
        // Calculate the multiples of lcm
        var multiples = (n / lcm) + 1;
 
        // Find the values which satisfies
        // the given condition
        var answer = Math.max(a, b) * multiples;
 
        // Subtract the extra values
        var lastvalue = lcm * (n / lcm) + Math.max(a, b);
        if (lastvalue > n)
            answer = answer - (lastvalue - n - 1);
 
        // Return the final result
        return answer;
    }
 
    function __gcd(a , b) {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Driver code
     
        var a = 1, b = 13, n = 500;
        document.write(findAns(a, b, n) + "\n");
 
// This code contributed by Rajput-Ji
</script>

Output: 
501

 

Time Complexity: O(log(min(a, b)), for using __gcd(a,b) method the overall time complexity is O(log(min(a,b))).
Auxiliary Space: O(1) because constant extra space is required.


Article Tags :