Given an array, find a product of all array elements.
Examples :
Input: arr[] = [1, 2, 3, 4]
Output: 120
Explanation: Product of array elements is 1 x 2 x 3 x 4 = 24
Input: arr[]= [100000, 100000, 100000]
Output: 993000007
Explanation: The product of the array elements is 100000 × 100000 × 100000 = 1000000000000000. Taking modulo 1000000007, the result is 1000000000000000 % 1000000007 = 993000007
Table of Content
Iterative Approach - O(n) Time and O(1) Space
The idea is to compute the product of all elements by traversing the array once and maintaining a running result.
- Initialize ans as 1
- One by one traverse array elements and multiply with the ans
- Return ans
#include <iostream>
#include <vector>
using namespace std;
int product(vector<int> &arr)
{
int n = arr.size();
int ans = 1;
for (int i = 0; i < n; i++)
ans = ans * arr[i];
return ans;
}
int main()
{
vector<int> arr = { 1, 2, 3, 4 };
cout << product(arr);
return 0;
}
#include <stdio.h>
// Function to find product of array elements
int product(int arr[], int n) {
int ans = 1;
for (int i = 0; i < n; i++)
ans = ans * arr[i];
return ans;
}
int main() {
int arr[] = { 1, 2, 3, 4 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", product(arr, n));
return 0;
}
class GfG {
// Function to find product of array elements
static int product(int[] arr) {
int n = arr.length;
int result = 1;
for (int i = 0; i < n; i++)
ans = ans * arr[i];
return ans;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4 };
System.out.println(product(arr));
}
}
def product(arr):
n = len(arr)
ans = 1
for i in range(n):
ans *= arr[i]
return ans
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 4]
print(product(arr))
using System;
class GfG
{
// Function to find product of array elements
static int Product(int[] arr)
{
int n = arr.Length;
int ans = 1;
for (int i = 0; i < n; i++)
ans = ans * arr[i];
return ans;
}
// Driver code
static void Main()
{
int[] arr = { 1, 2, 3, 4 };
Console.WriteLine(Product(arr));
}
}
function product(arr)
{
let n = arr.length;
let ans = 1;
for (let i = 0; i < n; i++) {
ans = ans * arr[i];
}
return ans;
}
// Driver code
let arr = [ 1, 2, 3, 4 ];
console.log(product(arr));
Output
120
Handling Overflows with Modulo Arithmetic - O(n) Time and O(1) Space
The above codes may cause overflow. Therefore, it is always desired to compute product under modulo. The reason for its working is the simple distributive property of modulo
( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
For example
Initial : arr[] = [100000, 100000, 100000], MOD = 1000000007, and = 1
Step 1 : Index = 1
ans = (1 * 100000) % MOD = 100000
Step 2 : Index = 2
result = (100000 × 100000) % MOD
= 10000000000 % 1000000007
= 999999937
Step 3 : Index = 3
result = (999999937 × 100000) % MOD
= 99999993700000 % 1000000007
= 993000007
Below is a program to find and print the product of all the number in this array of Modulo (10^9 +7).
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int product(vector<int> &arr)
{
int n = arr.size();
long long ans = 1;
for (int i = 0; i < n; i++)
ans = (ans * arr[i]) % MOD;
return ans;
}
int main()
{
vector<int> arr = {100000, 100000, 100000};
cout << product(arr);
return 0;
}
#include <stdio.h>
const int MOD = 1000000007;
// Function to find product of array elements under modulo
int product(int arr[], int n) {
long ans = 1;
for (int i = 0; i < n; i++)
ans = (ans * arr[i]) % MOD;
return ans;
}
int main() {
int arr[] = {100000, 100000, 100000};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", product(arr, n));
return 0;
}
class GfG {
// Modulo constant
static final int MOD = 1000000007;
// Function to find product of array elements under modulo
static int product(int[] arr) {
int n = arr.length;
int ans = 1;
for (int i = 0; i < n; i++)
ans = (int) ((ans * (long) arr[i]) % MOD);
return ans;
}
public static void main(String[] args) {
int[] arr = {100000, 100000, 100000};
System.out.println(product(arr));
}
}
MOD = 1000000007
# Function to find product of array elements under modulo
def product(arr):
n = len(arr)
ans = 1
for i in range(n):
ans = (ans * arr[i]) % MOD
return ans
# Driver code
if __name__ == "__main__":
arr = [100000, 100000, 100000]
print(product(arr))
using System;
class GfG
{
// Modulo constant
const int MOD = 1000000007;
// Function to find product of array elements under modulo
static int Product(int[] arr)
{
int n = arr.Length;
int ans = 1;
for (int i = 0; i < n; i++)
ans = (int)((ans * (long)arr[i]) % MOD);
return ans;
}
static void Main()
{
int[] arr = { 100000, 100000, 100000 };
Console.WriteLine(Product(arr));
}
}
let MOD = 1000000007;
// Function to find product of array elements under modulo
function product(arr)
{
let ans = 1;
let mod = 1000000007;
let n = arr.length;
for (let i = 0; i < n; i++) {
arr[i] = arr[i] % mod;
ans = (ans * arr[i]) % mod;
}
return ans;
}
// Driver code
let arr = [ 100000, 100000, 100000 ];
console.log(product(arr));
Output
993000007
Exercise Problems:
Sign of the product of a Array
Product of Array except itself.
Minimum steps to make the product of the array equal to 1