Arrays - Product of Array Except Self
Product of Array Except Self
The Product of Array Except Self problem involves computing an output array such that output[i] is equal to the product of all the elements of the input array except for input[i].
Video Explanation

Problem Statement
Given an array nums of length n, return an array output of the same length where output[i] is equal to the product of all the numbers in the input array except nums[i].
Constraints
- You must solve it in O(n) time complexity.
- You cannot use the division operation.
Instructions: Click the "Sort" button to visualize the Bubble Sort algorithm. You can also adjust the speed of the visualization using the slider.
Algorithm
- Create an output array of the same length as
nums. - Initialize a variable
leftto 1. - Populate the output array with the product of all elements to the left of each index.
- Initialize another variable
rightto 1. - Populate the output array with the product of all elements to the right of each index.
- Return the output array.
Pseudocode
Product of Array Except Self
procedure productExceptSelf( nums : list of integers )
n = length(nums)
output = array of size n
left = 1
for i = 0 to n - 1 do
output[i] = left
left *= nums[i]
right = 1
for i = n - 1 down to 0 do
output[i] *= right
right *= nums[i]
return output
end procedure
C++ Implementation:
// Product of Array Except Self
#include <iostream>
#include <vector>
using namespace std;
vector<int> sum(vector<int> &arr)
{
int n = arr.size();
vector<int> ans(n, 1); // Initialize ans array with 1's.
// Calculate prefix products
for (int i = 1; i < n; i++)
{
ans[i] = ans[i - 1] * arr[i - 1]; // ans[i] is the product of all elements before arr[i].
}
int suffix = 1; // Initialize suffix product.
// Calculate suffix products and multiply with prefix products
for (int i = n - 2; i >= 0; i--)
{
suffix *= arr[i + 1]; // Update suffix to be the product of elements after arr[i].
ans[i] *= suffix; // Multiply the current ans[i] (prefix product) by the suffix product.
}
return ans;
}
// Main Function
int main()
{
vector<int> arr = {1, 2, 3, 4};
vector<int> res = sum(arr); // calling function
for (int val : res)
{
cout << val << " ";
}
return 0;
}
JAVA Implementation:
public class ProductExceptSelf {
public static int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] output = new int[n];
// Step 1: Calculate prefix products
output[0] = 1; // The first prefix product is always 1
for (int i = 1; i < n; i++) {
output[i] = output[i - 1] * nums[i - 1];
}
// Step 2: Calculate suffix products and multiply with prefix
int suffix = 1;
for (int i = n - 1; i >= 0; i--) {
output[i] *= suffix;
suffix *= nums[i];
}
return output;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
int[] result = productExceptSelf(nums);
for (int value : result) {
System.out.print(value + " "); // Output: 24 12 8 6
}
}
}
Python Implementation:
def product_except_self(nums):
n = len(nums)
output = [1] * n
# Step 1: Calculate prefix products
for i in range(1, n):
output[i] = output[i - 1] * nums[i - 1]
# Step 2: Calculate suffix products and multiply with prefix
suffix = 1
for i in range(n - 1, -1, -1):
output[i] *= suffix
suffix *= nums[i]
return output
# Example usage
nums = [1, 2, 3, 4]
result = product_except_self(nums)
print(result) # Output: [24, 12, 8, 6]