Given two integers L and R, the task is to find the minimum possible value of (i * j) % 2019 where L ? i < j ? R
Examples:
Input: L = 2020, R = 2040
Output: 2
(2020*2021)%2019 = 2
Input: L = 3, R = 4
Output: 12
Approach:
- If R – L ? 2019 then answer is 0 as we will get a number in the range which is divisible by 2019 which gives remainder 0.
- If R – L < 2019 then we can run nested loops and find the minimum value.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
const int MOD = 2019;
// Function to return the minimum // possible value of (i * j) % 2019 int min_modulo( int l, int r)
{ // If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else {
// Find the minimum value
// by running nested loops
int ans = MOD - 1;
for ( int i = l; i <= r; i++) {
for ( int j = i + 1; j <= r; j++) {
ans = min(ans, (i * j) % MOD);
}
}
return ans;
}
} // Driver code int main()
{ int l = 2020, r = 2040;
cout << min_modulo(l, r);
return 0;
} |
Java
// Java implementation of the approach class GFG
{ static int MOD = 2019 ;
// Function to return the minimum // possible value of (i * j) % 2019 static int min_modulo( int l, int r)
{ // If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0 ;
else
{
// Find the minimum value
// by running nested loops
int ans = MOD - 1 ;
for ( int i = l; i <= r; i++)
{
for ( int j = i + 1 ; j <= r; j++)
{
ans = Math.min(ans, (i * j) % MOD);
}
}
return ans;
}
} // Driver code public static void main(String []args)
{ int l = 2020 , r = 2040 ;
System.out.println(min_modulo(l, r));
} } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach MOD = 2019 ;
# Function to return the minimum # possible value of (i * j) % 2019 def min_modulo(l, r) :
# If we can get a number
# divisible by 2019
if (r - l > = MOD) :
return 0 ;
else :
# Find the minimum value
# by running nested loops
ans = MOD - 1 ;
for i in range (l, r + 1 ) :
for j in range (i + 1 , r + 1 ) :
ans = min (ans, (i * j) % MOD);
return ans;
# Driver code if __name__ = = "__main__" :
l = 2020 ; r = 2040 ;
print (min_modulo(l, r));
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System;
class GFG
{ static int MOD = 2019;
// Function to return the minimum // possible value of (i * j) % 2019 static int min_modulo( int l, int r)
{ // If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else
{
// Find the minimum value
// by running nested loops
int ans = MOD - 1;
for ( int i = l; i <= r; i++)
{
for ( int j = i + 1; j <= r; j++)
{
ans = Math.Min(ans, (i * j) % MOD);
}
}
return ans;
}
} // Driver code public static void Main(String []args)
{ int l = 2020, r = 2040;
Console.WriteLine(min_modulo(l, r));
} } // This code is contributed by Rajput-Ji |
Javascript
<script> // JavaScript implementation of the approach
let MOD = 2019;
// Function to return the minimum
// possible value of (i * j) % 2019
function min_modulo(l, r)
{
// If we can get a number
// divisible by 2019
if (r - l >= MOD)
return 0;
else
{
// Find the minimum value
// by running nested loops
let ans = MOD - 1;
for (let i = l; i <= r; i++)
{
for (let j = i + 1; j <= r; j++)
{
ans = Math.min(ans, (i * j) % MOD);
}
}
return ans;
}
}
let l = 2020, r = 2040;
document.write(min_modulo(l, r));
</script> |
Output:
2
Time Complexity: O((r – l)2)
Auxiliary Space: O(1)