ADVERTISEMENT
ADVERTISEMENT

Python program to find remainder of array multiplication divided by divisor

Here, we will see a Python program to find the remainder of the product of array when divided by a divisor.
Submitted by Shivang Yadav, on April 04, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

In this program, we will take an array and divisor as input from the user and then print the value of array multiplication divided by divisor.

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).

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.

Python Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.