Given an array arr[] of size n and an integer k, find the remainder when the product of all elements of the array is divided by k.
Examples:Â
Input: arr[] = [4, 6, 7], k = 3
Output: 0
Explanation: 4 * 6 * 7 = 168 % 3 = 0.Input: arr[] = [1, 6], k = 5
Output: 1
Explanation: 1 * 6 = 6 % 5 = 1.
Table of Content
[Naive Approach] Compute Complete Product - O(n) Time and O(1) Space
Multiply all elements of the array to get the product and then return the remainder when this product is divided by k.
Working of Approach:
- Initialize a variable product as 1.
- Traverse the array and multiply every element with product.
- After processing all elements, compute product % k.
- Return the obtained remainder.
#include <bits/stdc++.h>
using namespace std;
int remArray(vector<int> &arr, int k)
{
// Stores the complete product.
long long product = 1;
// Multiply all array elements.
for (int x : arr)
{
product *= x;
}
// Return remainder after division by k.
return product % k;
}
int main()
{
vector<int> arr = {1, 6};
int k = 5;
cout << remArray(arr, k);
return 0;
}
import java.util.*;
class GFG {
static int remArray(int[] arr, int k)
{
// Stores the complete product.
long product = 1;
// Multiply all array elements.
for (int x : arr) {
product *= x;
}
// Return remainder after division by k.
return (int)(product % k);
}
public static void main(String[] args)
{
int[] arr = { 1, 6 };
int k = 5;
System.out.println(remArray(arr, k));
}
}
def remArray(arr, k):
# Stores the complete product.
product = 1
# Multiply all array elements.
for x in arr:
product *= x
# Return remainder after division by k.
return product % k
if __name__ == '__main__':
arr = [1, 6]
k = 5
print(remArray(arr, k))
using System;
public class GFG {
// Method to calculate the remainder of product of array
// elements divided by k.
public static int remArray(int[] arr, int k)
{
// Stores the complete product.
long product = 1;
// Multiply all array elements.
foreach(int x in arr) { product *= x; }
// Return remainder after division by k.
return (int)(product % k);
}
public static void Main()
{
int[] arr = { 1, 6 };
int k = 5;
Console.WriteLine(remArray(arr, k));
}
}
function remArray(arr, k)
{
// Stores the complete product.
let product = 1;
// Multiply all array elements.
for (let x of arr) {
product *= x;
}
// Return remainder after division by k.
return product % k;
}
// Driver Code
let arr = [ 1, 6 ];
let k = 5;
console.log(remArray(arr, k));
Output
1
[Expected Approach] Using Modular Multiplication - O(n) Time and O(1) Space
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Â
Working of Approach:
- Initialize the answer as
1. - Traverse each array element one by one.
- Update the answer as
(answer * currentElement) % k. - Repeat until all elements are processed.
- Return the final modulo value.
Let us understand with an example:
Input: arr[] = [1, 6], k = 5
- Initialize res = 1.
- Multiply the first element: res = (1 × 1) % 5 = 1.
- Multiply the second element: res = (1 × 6) % 5 = 6 % 5 = 1.
- All elements are processed while keeping the product modulo 5.
- Therefore, the remainder of the product is 1.
#include <bits/stdc++.h>
using namespace std;
int remArray(vector<int> &arr, int k)
{
int res = 1;
// Multiply each element while taking modulo.
for (int x : arr)
{
res = (res * x) % k;
}
// Return the final remainder.
return res;
}
int main()
{
vector<int> arr = {1, 6};
int k = 5;
cout << remArray(arr, k);
return 0;
}
import java.util.Arrays;
public class GFG {
public static int remArray(int[] arr, int k)
{
int res = 1;
// Multiply each element while taking modulo.
for (int x : arr) {
res = (res * x) % k;
}
// Return the final remainder.
return res;
}
public static void main(String[] args)
{
int[] arr = { 1, 6 };
int k = 5;
System.out.println(remArray(arr, k));
}
}
def remArray(arr, k):
res = 1
# Multiply each element while taking modulo.
for x in arr:
res = (res * x) % k
# Return the final remainder.
return res
if __name__ == "__main__":
arr = [1, 6]
k = 5
print(remArray(arr, k))
using System;
public class GFG {
public static int remArray(int[] arr, int k)
{
int res = 1;
// Multiply each element while taking modulo.
foreach(int x in arr) { res = (res * x) % k; }
// Return the final remainder.
return res;
}
public static void Main()
{
int[] arr = { 1, 6 };
int k = 5;
Console.WriteLine(remArray(arr, k));
}
}
function remArray(arr, k)
{
let res = 1;
// Multiply each element while taking modulo.
for (let x of arr) {
res = (res * x) % k;
}
// Return the final remainder.
return res;
}
// Driver Code
const arr = [ 1, 6 ];
const k = 5;
console.log(remArray(arr, k));
Output
1