Home » Python » Python programs

# 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: **

- Initialize prod = 1.
- Loop through the array for i -> 0 to n
- prod *= arr[i].

- 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:**

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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions