VOOZH about

URL: https://www.geeksforgeeks.org/dsa/product-array-puzzle-set-2-o1-space/

⇱ A product array puzzle | Set 2 (O(1) Space) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

A product array puzzle | Set 2 (O(1) Space)

Last Updated : 24 Apr, 2025

Given an array arr[] of size n, construct a product array res[] (of the same size) such that res[i] is equal to the product of all the elements of arr[] except arr[i]. 

Note: Solve it without division operator and in O(n).

Examples:

Input: arr[] = [10, 3, 5, 6, 2]
Output: [180, 600, 360, 300, 900]
Explanation:

  • For i=0, res[i] = 3 * 5 * 6 * 2 is 180.
  • For i = 1, res[i] = 10 * 5 * 6 * 2 is 600.
  • For i = 2, res[i] = 10 * 3 * 6 * 2 is 360.
  • For i = 3, res[i] = 10 * 3 * 5 * 2 is 300.
  • For i = 4, res[i] = 10 * 3 * 5 * 6 is 900.

Input: arr[] = [12, 0]
Output: [0, 12]
Explanation:

  • For i = 0, res[i] = 0.
  • For i = 1, res[i] = 12.

Refer to Product of Array Except Self | Set 1 for other approaches.

[Expected Approach - 1] Using Log Operator - O(n) time and O(1) space

The idea is to use log operator to find the product of all elements of the array except at a particular index.

How to use log operator for multiplication?

x = a * b * c * d
log(x) = log(a * b * c * d)
log(x) = log(a) + log(b) + log(c) + log(d)
x = antilog(log(a) + log(b) + log(c) + log(d))

Step by step approach:

  1. Traverse the array and find the sum of log of all the elements,   
    • log(a[0]) + log(a[1]) + .. + log(a[n-1])
  2. Then again traverse through the array and find the product using this formula.  
    • antilog((log(a[0]) + log(a[1]) + .. + log(a[n-1])) - log(a[i]))
  3. This equals to product of all the elements except a[i], i.e., antilog(sum- log(a[i])). 

How are negative numbers handled?

Log value of negative numbers is not defined. So we have to handle them separately.

To handle negative numbers, we take the log of the absolute value of each element and separately track the sign of the overall product. While computing the final result for each index, we adjust the sign based on whether the excluded element was negative, ensuring the correct sign is applied without affecting the logarithmic calculation.


Output
180 600 360 300 900 

[Expected Approach - 2] Using Power Function - O(n) time and O(1) space

The idea is to traverse the array and find the product of all the elements in the array. Then traverse the array again and find the product of all the elements except that number by using the formula (product * pow(a[i], -1)).

Step by step approach:

  1. Store the product of all values of array in a variable. Also find the number of zeros in the array.
  2. If the number of zeros is greater than 1, then return an array of zeros. If number of zeros is 1, then the index at which zero is present will have the value of the product of all values.
  3. If the number of zeros is 0, then for each value in the array, the product will be equal to (total product) * (power(arr[i], -1)).

Output
180 600 360 300 900 
Comment
Article Tags: