Open In App

Java Program to Find remainder of array multiplication divided by n

Last Updated : 18 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Write a Java program for a given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n.

Examples:

Input: arr[] = {100, 10, 5, 25, 35, 14},
n = 11
Output: 9
Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9

Input : arr[] = {100, 10},
n = 5
Output : 0
Explanation: 100 x 10 = 1000 % 5 = 0

Java Program to Find remainder of array multiplication divided by n using Naive approach:

First multiple all the number then take % by n then find the remainder, But in this approach, if the number is maximum of 2^64 then it give the wrong answer.

Below is the implementation of the above approach:

Java




public class Main {
    // This code finds the remainder of the product of all
    // the elements in the array arr divided by 'n'.
    public static int findRemainder(int[] arr, int len,
                                    int n)
    {
        int product = 1;
        for (int i = 0; i < len; i++) {
            product = product * arr[i];
        }
        return product % n;
    }
 
    public static void main(String[] args)
    {
        int[] arr = { 100, 10, 5, 25, 35, 14 };
        int len = arr.length;
        int n = 11;
        System.out.println(findRemainder(arr, len, n));
    }
}


Output

9

Time Complexity: O(n)
Auxiliary Space: O(1)

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

Below is the implementation of the above approach:

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


Output

9

Time Complexity: O(len)where len is the size of the given array
Auxiliary Space: O(1)

Please refer complete article on Find remainder of array multiplication divided by n for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads