Minimum possible value of (i * j) % 2019
Last Updated :
07 Mar, 2022
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++
#include <bits/stdc++.h>
using namespace std;
const int MOD = 2019;
int min_modulo( int l, int r)
{
if (r - l >= MOD)
return 0;
else {
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;
}
}
int main()
{
int l = 2020, r = 2040;
cout << min_modulo(l, r);
return 0;
}
|
Java
class GFG
{
static int MOD = 2019 ;
static int min_modulo( int l, int r)
{
if (r - l >= MOD)
return 0 ;
else
{
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;
}
}
public static void main(String []args)
{
int l = 2020 , r = 2040 ;
System.out.println(min_modulo(l, r));
}
}
|
Python3
MOD = 2019 ;
def min_modulo(l, r) :
if (r - l > = MOD) :
return 0 ;
else :
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;
if __name__ = = "__main__" :
l = 2020 ; r = 2040 ;
print (min_modulo(l, r));
|
C#
using System;
class GFG
{
static int MOD = 2019;
static int min_modulo( int l, int r)
{
if (r - l >= MOD)
return 0;
else
{
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;
}
}
public static void Main(String []args)
{
int l = 2020, r = 2040;
Console.WriteLine(min_modulo(l, r));
}
}
|
Javascript
<script>
let MOD = 2019;
function min_modulo(l, r)
{
if (r - l >= MOD)
return 0;
else
{
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>
|
Time Complexity: O((r – l)2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...