Q:

Python program to find remainder of array multiplication divided by divisor

belongs to collection: Python Array Programs

0

Example:

Input:
4
3	7	1	9 
4  

Output:
1

This problem can be solved easily by looping over the array and finding the product of all elements. Then finding the remainder of this product by N.

Algorithm:

  1. Initialize prod = 1.
  2. Loop through the array for i -> 0 to n
    • prod *= arr[i].
  3. Print the value of (prod % D).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

  1. Initialize prod = 1.
  2. Loop through the array for i -> 0 to n
    • prod *= (arr[i]%D).
  3. 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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Find the union and intersection of two arrays in P... >>
<< Python program for array rotation...