Given three integers **L**, **R**, and **N**, the task is to find the minimum possible value of **(i * j) % N**, where **L ≤ i < j ≤ R**.

**Examples:**

Input:L = 2020, R = 2040, N = 2019Output:2Explanation:(2020 * 2021) % 2019 = 2

Input:L = 15, R = 30, N = 15Output:0Explanation:If one of the elements of the pair is 15, then the product of all such pairs will be divisible by 15. Therefore, the remainder will be 0, which is minimum possible.

**Approach:** The given problem can be solved by finding the difference between **L** and **R**. If the difference is at least **N**, then the result will be **0**. Otherwise, iterate over the range **[L, R]** and find the minimum product.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `#define ll long long` `using` `namespace` `std;` `// Function to return the minimum` `// possible value of (i * j) % N` `void` `minModulo(` `int` `L, ` `int` `R, ` `int` `N)` `{` ` ` `if` `(R - L < N) {` ` ` `// Stores the minimum remainder` ` ` `int` `ans = INT_MAX;` ` ` `// Iterate from L to R` ` ` `for` `(ll i = L; i <= R; i++)` ` ` `// Iterate from L to R` ` ` `for` `(ll j = L; j <= R; j++)` ` ` `if` `(i != j)` ` ` `ans = min(0ll + ans,` ` ` `(i * j) % N);` ` ` `// Print the minimum value` ` ` `// of remainder` ` ` `cout << ans;` ` ` `}` ` ` `// If R - L >= N` ` ` `else` `{` ` ` `cout << 0;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `L = 6, R = 10, N = 2019;` ` ` `minModulo(L, R, N);` ` ` `return` `0;` `}` |

## Java

`// java program for the above approach` `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;` `public` `class` `GFG` `{` ` ` `// Function to return the minimum` ` ` `// possible value of (i * j) % N` ` ` `static` `void` `minModulo(` `int` `L, ` `int` `R, ` `int` `N)` ` ` `{` ` ` `if` `(R - L < N)` ` ` `{` ` ` `// Stores the minimum remainder` ` ` `int` `ans = Integer.MAX_VALUE;` ` ` `// Iterate from L to R` ` ` `for` `(` `int` `i = L; i <= R; i++)` ` ` `// Iterate from L to R` ` ` `for` `(` `int` `j = L; j <= R; j++)` ` ` `if` `(i != j)` ` ` `ans = Math.min(ans, (i * j) % N);` ` ` `// Print the minimum value` ` ` `// of remainder` ` ` `System.out.println(ans);` ` ` `}` ` ` `// If R - L >= N` ` ` `else` `{` ` ` `System.out.println(` `0` `);` ` ` `}` ` ` `}` ` ` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `L = ` `6` `, R = ` `10` `, N = ` `2019` `;` ` ` `minModulo(L, R, N);` ` ` `}` `}` `// This code is contributed by Kingash.` |

**Output:**

42

**Time Complexity:** O((R – L)^{2})**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**