Find count of numbers from 0 to n which satisfies the given equation for a value K
Last Updated :
30 Aug, 2022
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:
- We calculate (k % a) % b, in this case answer will always be (k % a) since the value of (k % a) will always be less than b.
- We calculate (k % b) % a, in this case also answer will always be (k % a) because (k % b) will return k as k is less than b.
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++
#include <bits/stdc++.h>
using namespace std;
int findAns( int a, int b, int n)
{
int lcm = (a * b) / __gcd(a, b);
int multiples = (n / lcm) + 1;
int answer = max(a, b) * multiples;
int lastvalue = lcm * (n / lcm) + max(a, b);
if (lastvalue > n)
answer = answer - (lastvalue - n - 1);
return answer;
}
int main()
{
int a = 1, b = 13, n = 500;
cout << findAns(a, b, n) << endl;
}
|
Java
class GFG{
static int findAns( int a, int b, int n)
{
int lcm = (a * b) / __gcd(a, b);
int multiples = (n / lcm) + 1 ;
int answer = Math.max(a, b) * multiples;
int lastvalue = lcm * (n / lcm) + Math.max(a, b);
if (lastvalue > n)
answer = answer - (lastvalue - n - 1 );
return answer;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void main(String[] args)
{
int a = 1 , b = 13 , n = 500 ;
System.out.print(findAns(a, b, n) + "\n" );
}
}
|
Python3
def findAns(a, b, n):
lcm = (a * b) / / __gcd(a, b);
multiples = (n / / lcm) + 1 ;
answer = max (a, b) * multiples;
lastvalue = lcm * (n / / lcm) + max (a, b);
if (lastvalue > n):
answer = answer - (lastvalue - n - 1 );
return answer;
def __gcd(a, b):
if (b = = 0 ):
return a;
else :
return __gcd(b, a % b);
if __name__ = = '__main__' :
a = 1 ;
b = 13 ;
n = 500 ;
print (findAns(a, b, n));
|
C#
using System;
class GFG{
static int findAns( int a, int b, int n)
{
int lcm = (a * b) / __gcd(a, b);
int multiples = (n / lcm) + 1;
int answer = Math.Max(a, b) * multiples;
int lastvalue = lcm * (n / lcm) + Math.Max(a, b);
if (lastvalue > n)
{
answer = answer - (lastvalue - n - 1);
}
return answer;
}
static int __gcd( int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
public static void Main(String[] args)
{
int a = 1, b = 13, n = 500;
Console.Write(findAns(a, b, n) + "\n" );
}
}
|
Javascript
<script>
function findAns(a , b , n) {
var lcm = (a * b) / __gcd(a, b);
var multiples = (n / lcm) + 1;
var answer = Math.max(a, b) * multiples;
var lastvalue = lcm * (n / lcm) + Math.max(a, b);
if (lastvalue > n)
answer = answer - (lastvalue - n - 1);
return answer;
}
function __gcd(a , b) {
return b == 0 ? a : __gcd(b, a % b);
}
var a = 1, b = 13, n = 500;
document.write(findAns(a, b, n) + "\n" );
</script>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...