Open In App
Related Articles

Python Program for Find remainder of array multiplication divided by n

Improve Article
Improve
Save Article
Save
Like Article
Like

Given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n.
Examples: 

Input : arr[] = {100, 10, 5, 25, 35, 14}, n = 11
Output : 9
Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9

Naive approach: First multiple all the numbers then take % by n to find the remainder, But in this approach, if the number is a maximum of 2^64 then it gives the wrong answer.

An approach that avoids overflow: First take a remainder or individual number like arr[i] % n. Then multiply the remainder with the current result. After multiplication, again take the remainder to avoid overflow. This works because of the distributive properties of modular arithmetic. ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c 

Python3




# to use reduce function import reduce from functools
from functools import reduce
 
 
def find_remainder(arr, n):
    sum_1 = reduce(lambda x, y: x*y, arr)
    remainder = sum_1 % n
    print(remainder)
 
 
arr = [100, 10, 5, 25, 35, 14]
n = 11
find_remainder(arr, n)

Output

9

Time Complexity: O(n), where n is the number of elements in the array. This is because the reduce function iterates through the entire array and performs the lambda function on each element.
Auxiliary Space: O(1), as the reduce function only requires a single variable to store the result.

Method2#: Without using inbuilt functions

Python3




arr = [100, 10, 5, 25, 35, 14];lens = len(arr);n = 11
mul = 1
for i in range(lens):
    mul = (mul * (arr[i] % n)) % n
print(mul % n)

Output

9

Method#3 : Using logarithmic operations –

In this approach we will use logarithmic operations to find out the remainder of array multiplication divided by a certain number. We will use the math module in this approach.

Step – 1 : We will import the math module.

Step – 2 : We will define a function which takes two variables as it’s argument the array and the element.

Step – 3 : Inside the function we will use a variable in which we will store the result of the logarithmic operation. We will iterate over the array index wise and find the log base 10 value of each element and add it with the previous result in the same variable.

Step – 4 : Then we will calculate the remainder by first finding the exponential of the variable using the pow() function by providing the base as 10 and then find the remainder. Here the reason of finding the exponential is to avoid direct multiplication of each array elements and then dividing the number by another element and finding the remainder, as if the numbers are large or the number of elements in the array is huge then we might face some overflow errors and the code will take a lot of time to execute. By converting them to log and adding them and then finding the exponential is same as of multiplying each element. But in this approach there is no chance of error.

Step – 5 : Finally we will return the remainder.

Python3




import math
 
def find_remainder(arr, n):
    # Calculating the sum of logarithms
    # of array elements and storing them
    # in the variable below
    log_sum = 0
    for i in arr:
        log_sum += math.log10(i)
     
    # Take exponential of the sum and return remainder
    remainder = int(pow(10, log_sum)) % n
    return remainder
 
# Example usage
arr = [100, 10, 5, 25, 35, 14]
ele = 11
rem = find_remainder(arr, ele)
print(rem)

Output

9
  • Time Complexity – O(n) # n is the length of the array.
  • Auxiliary Space – O(1) # no extra data structure has been used.

Please refer complete article on Find remainder of array multiplication divided by n for more details! 


Last Updated : 10 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials