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 = 2019
Output: 2
Explanation: (2020 * 2021) % 2019 = 2Input: L = 15, R = 30, N = 15
Output: 0
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:
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. |
Python3
# Python3 program for the above approach # Function to return the minimum # possible value of (i * j) % N def minModulo(L, R, N): if (R - L < N): # Stores the minimum remainder ans = 10 * * 9 # Iterate from L to R for i in range (L, R + 1 ): # Iterate from L to R for j in range (L, R + 1 ): if (i ! = j): ans = min (ans, (i * j) % N) # Print the minimum value # of remainder print (ans) # If R - L >= N else : print ( 0 ) # Driver Code if __name__ = = '__main__' : L, R, N = 6 , 10 , 2019 minModulo(L, R, N) # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System; 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 = Int32.MaxValue; // 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 Console.WriteLine(ans); } // If R - L >= N else { Console.WriteLine(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 ukasp |
Javascript
<script> // Javascript implementation // for the above approach // Function to return the minimum // possible value of (i * j) % N function minModulo(L, R, N) { if (R - L < N) { // Stores the minimum remainder let ans = Number.MAX_VALUE; // Iterate from L to R for (let i = L; i <= R; i++) // Iterate from L to R for (let j = L; j <= R; j++) if (i != j) ans = Math.min(ans, (i * j) % N); // Print the minimum value // of remainder document.write(ans); } // If R - L >= N else { document.write(0); } } // Driver Code let L = 6, R = 10, N = 2019; minModulo(L, R, N); // This code is contributed by susmitakundugoaldanga. </script> |
42
Time Complexity: O((R – L)2)
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!