Program to find the remainder of array multiplication divided by divisor

# Python program to find the remainder
# of array multiplication by divisor
# Function to find the remainder of
# array multiplication in Python
def findArrMulRemainder(arr, n, R):
prodVal = 1
for i in range(n):
prodVal *= arr[i]
return (prodVal % R)
# Taking array input from user
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
numbers = int(input())
arr.append(numbers)
D = int(input("Enter divisor: "))
rem = findArrMulRemainder(arr, n, D)
print("The remainder of array multiplication by divisor is ", rem)

Output:

Enter number of elements of the array: 4
Enter elements of the array:
5
7
2
1
Enter divisor: 4
The remainder of array multiplication by divisor is 2

Explanation:

In the above code, we have created a variable prodVal. And store the product of all elements of the array. Then we have returned the value of prodVal % d and printed it.

This solution is easy to implement but can lead to overflow as the product values can exceed the data's maximum value.

So, an effective approach would be using the commutative approach to solve the problem. This will save memory loss, and computation overhead.

This is based on: (a * b)%c = ((a%c) * (b%c))%c

We will find arr[i]%D for each element of the array and find their products. Then, find the remainder of this product with D.

Algorithm:

Initialize prod = 1.

Loop through the array for i -> 0 to n

prod *= (arr[i]%D).

Print the value of (prod % D).

Program to find the remainder of array multiplication divided by divisor

# Python program to find the remainder
# of array multiplication by divisor
# Function to find the remainder of
# array multiplication in Python
def findArrMulRemainder(arr, n, R):
prodVal = 1
for i in range(n):
prodVal *= (arr[i]%R)
return (prodVal % R)
# Taking array input from user
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
numbers = int(input())
arr.append(numbers)
D = int(input("Enter divisor: "))
rem = findArrMulRemainder(arr, n, D)
print("The remainder of array multiplication by divisor is ", rem)

Output:

Enter number of elements of the array: 4
Enter elements of the array:
5
7
2
1
Enter divisor: 4
The remainder of array multiplication by divisor is 2

Explanation:

In the above code, we have created a variable prodVal. And, store the product of (arr[i] % d) for all elements of the array. Then we have returned the value of prodVal % d and printed it.

Program to find the remainder of array multiplication divided by divisorOutput:Explanation:In the above code, we have created a variable

prodVal. And store the product of all elements of the array. Then we have returned the value ofprodVal % dand printed it.This solution is easy to implement but can lead to overflow as the product values can exceed the data's maximum value.

So, an effective approach would be using the commutative approach to solve the problem. This will save memory loss, and computation overhead.

This is based on:

(a * b)%c = ((a%c) * (b%c))%cWe will find

arr[i]%Dfor each element of the array and find their products. Then, find the remainder of this product withD.Algorithm:Program to find the remainder of array multiplication divided by divisorOutput:Explanation:In the above code, we have created a variable

need an explanation for this answer? contact us directly to get an explanation for this answerprodVal. And, store the product of (arr[i] % d) for all elements of the array. Then we have returned the value ofprodVal % dand printed it.