I've been able to find sum of all subarrays, product of all subarrays, product of the sum of all subarrays in O(n) but I'm not able to find a pattern for this.
Explanation:
array = {1,2,3}
ans = (1)*(2)*(3)*(1+2)*(2+3)*(1+2+3) = 540
There can be 10e5 elements in the array.
Can someone please help me find a way to solve this in linear time?
Thank you!
I don't why you're so confused with the question. Simply the question goes as follows that we need to calculate product of sum of all subarrays (example given in previous replies and the post itself.) I've already wasted around a week trying to figure out a way to get this in linear time but I didn't succeed. That's why I'm asking someone to help me I'm getting nowhere in this question.
Constraints:
array size: 1 < size < 10e5
elements: 0 <= element <= 10e9
Time complexity: <= O(nlogn)
Your elements can be of size 109. There could be up to 105 of them. And you seriously think you are going to represent their product in an integer variable?
Or are you going to give your answer modulo something?
State the whole problem. State which particular competition it comes from.
You are obviously the one who is totally confused here.
I know exactly what you are looking for.
You are looking for the "product of the sum of all subarrays".
That's what you are looking for, whether you know it or not.
(I can't fathom why you don't know that! Jesus!)
Anyway, you also state in your original braindead question that you already know the "product of the sum of all subarrays".
Now can your tiny brain understand the problem?
There are N(N+1)/2 subarrays so you have that many factors. Clearly multiplying them is O(N2) so to do this faster, you need some way to reduce the factors. I've been playing around with this a little and I can't figure anything out.
What makes you think this can be done in O(N) time?