Given an integer array, replace each element with the product of every other element without using the division operator.

For example,

Input:  { 1, 2, 3, 4, 5 }
Output: { 120, 60, 40, 30, 24 }
 
 
Input:  { 5, 3, 4, 2, 6, 8 }
Output: { 1152, 1920, 1440, 2880, 960, 720 }

Practice this problem

A naive solution would be to calculate the product of all elements in the left and right subarray for each array element. The time complexity of this approach is O(n2), where n is the size of the input.

 
We can solve this problem in linear time by using two auxiliary arrays, left[] and right[], where left[i] stores the product of all elements in subarray A[0…i-1] and right[i] stores the product of all elements in subarray A[i+1…n-1]. Now for each element A[i], replace it with the product of its left-subarray and right-subarray (i.e., A[i] = left[i] × right[i]).

The algorithm can be implemented as follows in C, Java, and Python:

C


Download  Run Code

Output:

1152 1920 1440 2880 960 720

Java


Download  Run Code

Output:

Python


Download  Run Code

The time complexity of the above solution is O(n) and requires O(n) extra space.

 
We can use recursion to solve this problem in linear time and constant space. The idea is to recursively calculate all elements’ products in the right subarray and pass the left-subarray product in function arguments. Following is the C, Java, and Python program that demonstrates it:

C


Download  Run Code

Output:

1152 1920 1440 2880 960 720

Java


Download  Run Code

Output:

[1152, 1920, 1440, 2880, 960, 720]

Python


Download  Run Code

Output:

[1152, 1920, 1440, 2880, 960, 720]

The time complexity of the above solution is O(n) and requires O(n) implicit space for the call stack.