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 = 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

2

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Rajput-Ji