Find remainder of array multiplication divided by n
Given multiple numbers and a number n, the task is to print the remainder after multiply all the number divide by n.
Examples:
Input : arr[] = {100, 10, 5, 25, 35, 14},
n = 11
Output : 9
100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9
Input : arr[] = {100, 10},
n = 5
Output : 0
100 x 10 = 1000 % 5 = 0
Naive approach: First multiple all the number then take % by n then find the remainder, But in this approach if number is maximum of 2^64 then it give wrong answer.
Approach that avoids overflow : First take a remainder or individual number like arr[i] % n. Then multiply the remainder with current result. After multiplication, again take remainder to avoid overflow. This works because of distributive properties of modular arithmetic. ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
C++
// C++ program to find // remainder when all // array elements are // multiplied. #include <iostream> using namespace std; // Find remainder of arr[0] * arr[1] * // .. * arr[n-1] int findremainder(int arr[], int len, int n) { int mul = 1; // find the individual remainder // and multiple with mul. for (int i = 0; i < len; i++) mul = (mul * (arr[i] % n)) % n; return mul % n; } // Driver code int main() { int arr[] = { 100, 10, 5, 25, 35, 14 }; int len = sizeof(arr) / sizeof(arr[0]); int n = 11; // print the remainder of after // multiple all the numbers cout << findremainder(arr, len, n); } |
Java
// Java program to find // remainder when all // array elements are // multiplied. import java.util.*; import java.lang.*; public class GfG{ // Find remainder of arr[0] * arr[1] * // .. * arr[n-1] public static int findremainder(int arr[], int len, int n) { int mul = 1; // find the individual remainder // and multiple with mul. for (int i = 0; i < len; i++) mul = (mul * (arr[i] % n)) % n; return mul % n; } // Driver function public static void main(String argc[]) { int[] arr = new int []{ 100, 10, 5, 25, 35, 14 }; int len = 6; int n = 11; // print the remainder of after // multiple all the numbers System.out.println(findremainder(arr, len, n)); } } /* This code is contributed by Sagar Shukla */ |
Python3
# Python3 program to # find remainder when # all array elements # are multiplied. # Find remainder of arr[0] * arr[1] # * .. * arr[n-1] def findremainder(arr, lens, n): mul = 1 # find the individual # remainder and # multiple with mul. for i in range(lens): mul = (mul * (arr[i] % n)) % n return mul % n # Driven code arr = [ 100, 10, 5, 25, 35, 14 ] lens = len(arr) n = 11 # print the remainder # of after multiple # all the numbers print( findremainder(arr, lens, n)) # This code is contributed by "rishabh_jain". |
C#
// C# program to find // remainder when all // array elements are // multiplied. using System; public class GfG{ // Find remainder of arr[0] * arr[1] * // .. * arr[n-1] public static int findremainder(int []arr, int len, int n) { int mul = 1; // find the individual remainder // and multiple with mul. for (int i = 0; i < len; i++) mul = (mul * (arr[i] % n)) % n; return mul % n; } // Driver function public static void Main() { int[] arr = new int []{ 100, 10, 5, 25, 35, 14 }; int len = 6; int n = 11; // print the remainder of after // multiple all the numbers Console.WriteLine(findremainder(arr, len, n)); } } /* This code is contributed by vt_m */ |
PHP
<?php // PHP program to find // remainder when all // array elements are // multiplied. // Find remainder of arr[0] * arr[1] * // .. * arr[n-1] function findremainder($arr, $len, $n) { $mul = 1; // find the individual remainder // and multiple with mul. for ($i = 0; $i < $len; $i++) $mul = ($mul * ($arr[$i] % $n)) % $n; return $mul % $n; } // Driver code $arr = array(100, 10, 5, 25, 35, 14); $len = sizeof($arr); $n = 11; // print the remainder of after // multiple all the numbers echo(findremainder($arr, $len, $n)); // This code is contributed by Ajit. ?> |
Output:
9
Recommended Posts:
- Program to find remainder when large number is divided by r
- Program to find remainder when large number is divided by 11
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Find if array can be divided into two subarrays of equal sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find the element whose multiplication with -1 makes array sum 0
- Find N % 4 (Remainder with 4) for a large value of N
- Program to find remainder without using modulo or % operator
- Check if a sorted array can be divided in pairs whose sum is k
- Check if an array can be divided into pairs whose sum is divisible by k
- Check if an array of 1s and 2s can be divided into 2 parts with equal sum
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Program for multiplication of array elements
- Right most non-zero digit in multiplication of array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



