Given an integer a, b, m. Find (a * b ) mod m, where a, b may be large and their direct multiplication may cause overflow. However they are smaller than half of the maximum allowed long long int value.
Input: a = 426, b = 964, m = 235 Output: 119 Explanation: (426 * 964) % 235 = 410664 % 235 = 119 Input: a = 10123465234878998, b = 65746311545646431 m = 10005412336548794 Output: 4652135769797794
A naive approach is to use arbitrary precision data type such as int in python or Biginteger class in Java. But that approach will not be fruitful because internal conversion of string to int and then perform operation will lead to slow down the calculations of addition and multiplications in binary number system.
Efficient solution : Since a and b may be very large numbers, if we try to multiply directly then it will definitely overflow. Therefore we use the basic approach of multiplication i.e.,
a * b = a + a + … + a (b times)
So we can easily compute the value of addition (under modulo m) without any
overflow in the calculation. But if we try to add the value of a repeatedly up to b times then it will definitely timeout for the large value of b, since the time complexity of this approach would become O(b).
So we divide the above repeated steps of a in simpler way i.e.,
If b is even then a * b = 2 * a * (b / 2), otherwise a * b = a + a * (b - 1)
Below is the approach describing above explanation :
Time complexity: O(log b)
Auxiliary space: O(1)
Note: Above approach will only work if 2 * m can be represent in standard data type otherwise it will lead to overflow.
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
- Compute n! under modulo p
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Equalizing array using increment under modulo 3
- Expressing a fraction as a natural number under modulo 'm'
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
- Multiply Large Numbers represented as Strings
- Multiply Large Numbers using Grid Method
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Modulo power for large numbers represented as strings
- Modulo of a large Binary String
- Number of factors of very large number N modulo M where M is any prime number
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Efficient way to multiply with 7
- Russian Peasant (Multiply two numbers using bitwise operators)
- Find the smallest number whose digits multiply to a given number n