Given an array, find a product of all array elements.
Examples :
Input : ar[] = {1, 2, 3, 4, 5}
Output : 120
Product of array elements is 1 x 2
x 3 x 4 x 5 = 120.
Input : ar[] = {1, 6, 3}
Output : 18
Implementation:
Two Pointer Approach:
Approach:
1) Input: arr[]
2) Initialize with start and last pointers i.e i,j. and also initialize product=0
3) Iterate i=0 to i>j;
i+=1
j-=1
4) Multiply first and last numbers at a time while iterating.
5) if i==j multiply element only once.
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int product = 1;
int i = 0;
int j = sizeof (arr) / sizeof (arr[0]) - 1;
while (i < j) {
product *= arr[i] * arr[j];
i++;
j--;
}
if (i == j) {
product *= arr[i];
}
cout << product << endl;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int product = 1 ;
int i = 0 ;
int j = arr.length- 1 ;
while (i < j) {
product *= arr[i] * arr[j];
i++;
j--;
}
if (i == j) {
product *= arr[i];
}
System.out.println( product);
}
}
|
Python3
arr = [ 1 , 2 , 3 , 4 , 5 ]
product = 1
i = 0
j = len (arr) - 1
while (i<j):
product * = arr[i] * arr[j]
i + = 1
j - = 1
if (i = = j):
product * = arr[i]
print (product)
|
C#
using System;
public class GFG {
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3, 4, 5 };
int product = 1;
int i = 0;
int j = arr.Length - 1;
while (i < j) {
product *= arr[i] * arr[j];
i += 1;
j -= 1;
}
if (i == j)
product *= arr[i];
Console.WriteLine(product);
}
}
|
Javascript
let arr = [1,2,3,4,5];
let product = 1;
let i = 0;
let j = arr.length-1;
while (i < j)
{
product *= arr[i]*arr[j];
i += 1;
j -= 1;
}
if (i == j)
product *= arr[i];
console.log(product);
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Implementation:
C++
#include <iostream>
using namespace std;
int product( int ar[], int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << product(ar, n);
return 0;
}
|
C
#include <stdio.h>
int product( int ar[], int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "%d" , product(ar, n));
return 0;
}
|
Java
import java.io.*;
public class GFG{
static int product( int ar[], int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
result = result * ar[i];
return result;
}
public static void main(String[] args)
{
int ar[] = { 1 , 2 , 3 , 4 , 5 };
int n = ar.length;
System.out.printf( "%d" , product(ar, n));
}
}
|
Python3
def product(ar, n):
result = 1
for i in range ( 0 , n):
result = result * ar[i]
return result
ar = [ 1 , 2 , 3 , 4 , 5 ]
n = len (ar)
print (product(ar, n))
|
C#
using System;
class GFG {
static int product( int []ar, int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * ar[i];
return result;
}
public static void Main()
{
int []ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(product(ar, n));
}
}
|
PHP
<?php
function product( $ar , $n )
{
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
$result = $result * $ar [ $i ];
return $result ;
}
$ar = array ( 1, 2, 3, 4, 5 );
$n = count ( $ar );
print ((int)product( $ar , $n ));
?>
|
Javascript
<script>
function product(ar,n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = result * ar[i];
return result;
}
let ar = [ 1, 2, 3, 4, 5 ];
let n = ar.length;
document.write(parseInt(product(ar, n)));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
The above code 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
Below is a program to find and print the product of all the number in this array of Modulo (10^9 +7).
Implementation:
C++
#include <iostream>
using namespace std;
const int MOD = 1000000007;
int product( int ar[], int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
cout << product(ar, n);
return 0;
}
|
C
#include <stdio.h>
const int MOD = 1000000007;
int product( int ar[], int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
int main()
{
int ar[] = { 1, 2, 3, 4, 5 };
int n = sizeof (ar) / sizeof (ar[0]);
printf ( "%d" , product(ar, n));
return 0;
}
|
Java
import java.io.*;
public class GFG {
static final int MOD = 1000000007 ;
static int product( int ar[], int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
public static void main(String[] args)
{
int ar[] = { 1 , 2 , 3 , 4 , 5 };
int n = ar.length;
System.out.printf( "%d" , product(ar, n));
}
}
|
Python3
MOD = 1000000007
def product(ar, n):
result = 1
for i in range ( 0 , n):
result = (result * ar[i]) % MOD
return result
ar = [ 1 , 2 , 3 , 4 , 5 ]
n = len (ar)
print (product(ar, n))
|
C#
using System;
class GFG {
static int MOD = 1000000007;
static int product( int []ar, int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
public static void Main()
{
int []ar = { 1, 2, 3, 4, 5 };
int n = ar.Length;
Console.WriteLine(product(ar, n));
}
}
|
PHP
<?php
function product( $ar , $n )
{
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
$result = ( $result *
$ar [ $i ]) % 1000000007;
return $result ;
}
$ar = array ( 1, 2, 3, 4, 5 );
$n = count ( $ar );
print (product( $ar , $n ));
?>
|
Javascript
<script>
let MOD = 1000000007;
function product(ar, n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = (result * ar[i]) % MOD;
return result;
}
let ar = [ 1, 2, 3, 4, 5 ];
let n = ar.length;
document.write(product(ar, n));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Shivani Baghel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.