Minimum possible value of (i * j) % 2019
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 = 2Input: 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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output:
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.