Open In App

JavaScript Program to Find Remainder of Array Multiplication Divided by n

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the remainder of the array multiplication when divided by the number n can be found by iteration, functional programming or array reduction. , it’s often necessary to calculate the remainder of the product of all elements in an array when divided by a given number ‘n’. There are various possible approaches in JavaScript to achieve this which are as follows:

Using forEach() method

This approach iterates through each element of the array using the forEach() method and calculates the remainder of the product of all elements when divided by a given number ‘n’.

The forEach() method iterates over elements of an array and executes a provided function once for each array element. It allows you to perform a specific action on each element of the array without mutating the array itself.

Example: To demonstrate finding the remainder of the array multiplication using forEach() method.

Javascript




function findRemainder1(arr, n) {
    let product = 1;
    arr.forEach((num) => (product *= num));
    return product % n;
}
 
let arr1 = [3, 7, 2, 5];
let n1 = 11;
console.log(findRemainder1(arr1, n1));


Output

1

Time Complexity: O(n)

Space Complexity: O(1)

Using reduce() function

This method uses the reduce() function to accumulate the product of all array elements and then calculates the remainder when divided by a specified number ‘n’.

The reduce() method executes a reducer function on each element of the array, resulting in a single output value. It accumulates the result of the function calls on each element, providing the final result.

Example: This example illustrates finding the remainder of the array multiplication divided by ‘n’ in JavaScript using the reduce() method.

Javascript




function findRemainder2(arr, n) {
    let product = arr.reduce((acc, val) => {
        acc * val, 1
    });
    return product % n;
}
 
let arr2 = [4, 8, 6, 2];
let n2 = 13;
console.log(findRemainder2(arr2, n2));


Output

NaN

Time Complexity: O(n)

Space Complexity: O(1)

Using functional approach with reduce()

In this approach, a functional programming style is employed along with the reduce() function to find the remainder of the array multiplication when divided by a given number ‘n’.

Example: Here, the code showcases how to find the remainder of the array multiplication divided by ‘n’ in JavaScript using the reduce() method with a functional approach.

Javascript




function findRemainder3(arr, n) {
    return arr.reduce((acc, val) => {
        (acc * val) % n, 1
    });
}
 
 
let arr3 = [1, 2, 3, 4];
let n3 = 17;
console.log(findRemainder3(arr3, n3));


Output

undefined

Time Complexity: O(n)

Space Complexity: O(1)

Using traditional for-loop

This approach utilizes a traditional for-loop to traverse the array and compute the product of all elements, followed by finding the remainder when divided by a provided number ‘n’.

Example: To demonstrates how to find the remainder of the array multiplication divided by ‘n’ in JavaScript using a for loop.

Javascript




function findRemainder4(arr, n) {
    let product = 1;
    for (let num of arr) {
        product *= num;
        product %= n;
    }
    return product;
}
 
let arr4 = [2, 4, 6, 8];
let n4 = 19;
console.log(findRemainder4(arr4, n4));


Output

4

Time Complexity: O(n)

Space Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads