A Product Array Puzzle | Set 3
Last Updated :
26 Sep, 2021
Given an array arr[] consisting of N integers, the task is to construct a Product array of the same size without using division (‘/’) operator such that each array element becomes equal to the product of all the elements of arr[] except arr[i].
Examples:
Input: arr[] = {10, 3, 5, 6, 2}
Output: 180 600 360 300 900
Explanation:
3 * 5 * 6 * 2 is the product of all array elements except 10 is 180
10 * 5 * 6 * 2 is the product of all array elements except 3 is 600.
10 * 3 * 6 * 2 is the product of all array elements except 5 is 360.
10 * 3 * 5 * 2 is the product of all array elements except 6 is 300.
10 * 3 * 6 * 5 is the product of all array elements except 2 is 9.
Input: arr[] = {1, 2, 1, 3, 4}
Output: 24 12 24 8 6
Approach: The idea is to use log() and exp() functions instead of log10() and pow(). Below are some observations regarding the same:
- Suppose M is the multiplication of all the array elements then the element of output array at ith position will be equal M/arr[i].
- The divisions of two numbers can be performed by using the property of logarithm and exp functions.
- The logarithmic function is not defined for numbers less than zero so to maintain the such cases separately.
Follow the steps below to solve the problem:
- Initialize two variables, say product = 1 and Z = 1, to store the product of array and count of zero elements.
- Traverse the array and multiply the product by arr[i] if arr[i] is not equal to 0. Otherwise, increment count of Z by one.
- Traverse the array arr[] and perform the following:
- If Z is 1 and arr[i] is not zero then update arr[i] as arr[i] = 0 and continue.
- Otherwise, if Z is 1 and arr[i] is 0 then update arr[i] as product and continue.
- Otherwise, if Z is greater than 1 then assign arr[i] as 0 and continue.
- Now find the value of abs(product)/abs(arr[i]) using the formula discussed above and store it in a variable say curr.
- If the value of arr[i] and product is negative or if arr[i] and product is positive then assign arr[i] as curr.
- Otherwise, assign arr[i] as -1*curr.
- After completing the above steps, print the array arr[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void productExceptSelf( int arr[],
int N)
{
int product = 1;
int z = 0;
for ( int i = 0; i < N; i++) {
if (arr[i])
product *= arr[i];
z += (arr[i] == 0);
}
int a = abs (product), b;
for ( int i = 0; i < N; i++) {
if (z == 1) {
if (arr[i])
arr[i] = 0;
else
arr[i] = product;
continue ;
}
else if (z > 1) {
arr[i] = 0;
continue ;
}
int b = abs (arr[i]);
int curr = round( exp ( log (a) - log (b)));
if (arr[i] < 0 && product < 0)
arr[i] = curr;
else if (arr[i] > 0 && product > 0)
arr[i] = curr;
else
arr[i] = -1 * curr;
}
for ( int i = 0; i < N; i++) {
cout << arr[i] << " " ;
}
}
int main()
{
int arr[] = { 10, 3, 5, 6, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
productExceptSelf(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void productExceptSelf( int arr[],
int N)
{
int product = 1 ;
int z = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] != 0 )
product *= arr[i];
if (arr[i] == 0 )
z += 1 ;
}
int a = Math.abs(product);
for ( int i = 0 ; i < N; i++) {
if (z == 1 ) {
if (arr[i] != 0 )
arr[i] = 0 ;
else
arr[i] = product;
continue ;
}
else if (z > 1 ) {
arr[i] = 0 ;
continue ;
}
int b = Math.abs(arr[i]);
int curr = ( int )Math.round(Math.exp(Math.log(a) - Math.log(b)));
if (arr[i] < 0 && product < 0 )
arr[i] = curr;
else if (arr[i] > 0 && product > 0 )
arr[i] = curr;
else
arr[i] = - 1 * curr;
}
for ( int i = 0 ; i < N; i++) {
System.out.print(arr[i] + " " );
}
}
public static void main(String args[])
{
int arr[] = { 10 , 3 , 5 , 6 , 2 };
int N = arr.length;
productExceptSelf(arr, N);
}
}
|
Python3
import math
def productExceptSelf(arr, N) :
product = 1
z = 0
for i in range (N):
if (arr[i] ! = 0 ) :
product * = arr[i]
if (arr[i] = = 0 ):
z + = 1
a = abs (product)
for i in range (N):
if (z = = 1 ) :
if (arr[i] ! = 0 ) :
arr[i] = 0
else :
arr[i] = product
continue
elif (z > 1 ) :
arr[i] = 0
continue
b = abs (arr[i])
curr = round (math.exp(math.log(a) - math.log(b)))
if (arr[i] < 0 and product < 0 ):
arr[i] = curr
elif (arr[i] > 0 and product > 0 ):
arr[i] = curr
else :
arr[i] = - 1 * curr
for i in range (N):
print (arr[i], end = " " )
arr = [ 10 , 3 , 5 , 6 , 2 ]
N = len (arr)
productExceptSelf(arr, N)
|
C#
using System;
class GFG
{
static void productExceptSelf( int [] arr,
int N)
{
int product = 1;
int z = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] != 0)
product *= arr[i];
if (arr[i] == 0)
z += 1;
}
int a = Math.Abs(product);
for ( int i = 0; i < N; i++)
{
if (z == 1)
{
if (arr[i] != 0)
arr[i] = 0;
else
arr[i] = product;
continue ;
}
else if (z > 1)
{
arr[i] = 0;
continue ;
}
int b = Math.Abs(arr[i]);
int curr = ( int )Math.Round(Math.Exp(Math.Log(a) - Math.Log(b)));
if (arr[i] < 0 && product < 0)
arr[i] = curr;
else if (arr[i] > 0 && product > 0)
arr[i] = curr;
else
arr[i] = -1 * curr;
}
for ( int i = 0; i < N; i++)
{
Console.Write(arr[i] + " " );
}
}
public static void Main(String[] args)
{
int [] arr = { 10, 3, 5, 6, 2 };
int N = arr.Length;
productExceptSelf(arr, N);
}
}
|
Javascript
<script>
function productExceptSelf(arr,
N)
{
let product = 1;
let z = 0;
for (let i = 0; i < N; i++) {
if (arr[i] != 0)
product *= arr[i];
if (arr[i] == 0)
z += 1;
}
let a = Math.abs(product);
for (let i = 0; i < N; i++) {
if (z == 1) {
if (arr[i] != 0)
arr[i] = 0;
else
arr[i] = product;
continue ;
}
else if (z > 1) {
arr[i] = 0;
continue ;
}
let b = Math.abs(arr[i]);
let curr = Math.round(Math.exp(Math.log(a) - Math.log(b)));
if (arr[i] < 0 && product < 0)
arr[i] = curr;
else if (arr[i] > 0 && product > 0)
arr[i] = curr;
else
arr[i] = -1 * curr;
}
for (let i = 0; i < N; i++) {
document.write(arr[i] + " " );
}
}
let arr = [ 10, 3, 5, 6, 2 ];
let N = arr.length;
productExceptSelf(arr, N);
</script>
|
Output: 180 600 360 300 900
Time Complexity: O(N)
Auxiliary Space: O(1)
Alternate Approaches: Please refer to the previous posts of this article for alternate approaches:
Share your thoughts in the comments
Please Login to comment...