Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. Solve it without division operator in O(n) time.
Example :
Input: arr[] = {10, 3, 5, 6, 2}
Output: prod[] = {180, 600, 360, 300, 900}
3 * 5 * 6 * 2 product of other array
elements except 10 is 180
10 * 5 * 6 * 2 product of other array
elements except 3 is 600
10 * 3 * 6 * 2 product of other array
elements except 5 is 360
10 * 3 * 5 * 2 product of other array
elements except 6 is 300
10 * 3 * 6 * 5 product of other array
elements except 2 is 900
Input: arr[] = {1, 2, 3, 4, 5}
Output: prod[] = {120, 60, 40, 30, 24 }
2 * 3 * 4 * 5 product of other array
elements except 1 is 120
1 * 3 * 4 * 5 product of other array
elements except 2 is 60
1 * 2 * 4 * 5 product of other array
elements except 3 is 40
1 * 2 * 3 * 5 product of other array
elements except 4 is 30
1 * 2 * 3 * 4 product of other array
elements except 5 is 24Naive Solution:
Approach:
Create two extra space, i.e. two extra arrays to store the product of
all the array elements from start, up to that index and another array to
store the product of all the array elements from the end of the array
to that index.
To get the product excluding that index, multiply the prefix product up to index i-1 with the suffix product up to index i+1.
Algorithm:
- Create two array prefix and suffix of length n, i.e length of the original array, initialize prefix[0] = 1 and suffix[n-1] = 1 and also another array to store the product.
- Traverse the array from second index to end.
- For every index i update prefix[i] as prefix[i] = prefix[i-1] * array[i-1], i.e store the product upto i-1 index from the start of array.
- Traverse the array from second last index to start.
- For every index i update suffix[i] as suffix[i] = suffix[i+1] * array[i+1], i.e store the product upto i+1 index from the end of array
- Traverse the array from start to end.
- For every index i the output will be prefix[i] * suffix[i], the product of the array element except that element.
The product array is: 180 600 360 300 900
Complexity Analysis:
- Time Complexity: O(n).
The array needs to be traversed three times, so the time complexity is O(n). - Space Complexity: O(n).
Two extra arrays and one array to store the output is needed so the space complexity is O(n)
Note: The above method can be optimized to work in space complexity O(1). Thanks to Dileep for suggesting the below solution.
Efficient solution:
Approach:
In the previous solution, two extra arrays were created to store the
prefix and suffix, in this solution store the prefix and suffix product
in the output array (or product array) itself. Thus reducing the space
required.
Algorithm:
- Create an array product and initialize its value to 1 and a variable temp = 1.
- Traverse the array from start to end.
- For every index i update product[i] as product[i] = temp and temp = temp * array[i], i.e store the product upto i-1 index from the start of array.
- initialize temp = 1 and traverse the array from last index to start.
- For every index i update product[i] as product[i] = product[i] * temp and temp = temp * array[i], i.e multiply with the product upto i+1 index from the end of array.
- Print the product array.
The product array is: 180 600 360 300 900
Complexity Analysis:
- Time Complexity: O(n).
The original array needs to be traversed only once, so the time complexity is constant. - Space Complexity: O(n).
Even though the extra arrays are removed, the space complexity remains O(n), as the product array is still needed.
Another Approach:
Store the product of all the elements is a variable and then iterate the array and add product/current_index_value in a new array. and then return this new array.
Below is the implementation of the above approach:
180 600 360 300 900
Time Complexity: O(n)
Space Complexity: O(1)
The original array needs to be traversed only once, so the space complexity is constant.
No comments:
Post a Comment