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.
Input: L = 2020, R = 2040, N = 2019
Explanation: (2020 * 2021) % 2019 = 2
Input: L = 15, R = 30, N = 15
Explanation: 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:
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.