You are given 2 arrays. Choose a subarray from the first array (arr1) and replace it with the corresponding subarray from the second array (arr2). After replacing the subarray, calculate the maximum consecutive subarray sum of the modified first array. Find the maximum subarray sum you can get by doing this operation. You can do this operation only once.
N is 10^5 and ai [-1e9, 1e9]
Im not able to think of better than n^2. Pls help
Auto comment: topic has been updated by NoobCoder1998 (previous revision, new revision, compare).
Kadane along with sliding window should do the trick in O(N)
Ps: Can you provide problem name or number.
This was asked to me in Online assessment so cant provide link.. can u share ur approach
The best solution I can think of:
You can solve it using DP. Let $$$dp[i][x]$$$ be the maximum sum of a subarray starting at index $$$i$$$, where:
If $$$x=0$$$, we can:
If $$$x=1$$$, we can:
If $$$x=2$$$, we can:
So:
$$$dp[i][0]=max(dp[i][1],dp[i+1][0]+arr1[i],0)$$$
$$$dp[i][1]=max(dp[i][2],dp[i+1][1]+arr2[i],0)$$$
$$$dp[i][2]=max(dp[i+1][2]+arr1[i],0)$$$
Base case: $$$dp[n][x]=0$$$
The answer is the maximum value of $$$dp[i][0]$$$ for all $$$i$$$.
Total complexity: $$$O(n)$$$
Hope it helps!
Thanks for the solution.. after some thinking i came up with one approach
Replacing a subarray in arr1 with a corresponding subarray from arr2 is equivalent to calculating the difference between the subarrays in arr2 and arr1. For example, let arr1 = [1,2,3], arr2 = [1,4,5]. If I want to replace subarray[2,3] with [4,5] that will change the subarray sum from 5 to 9, increase by 4. by calculating difference of values in the subarray also you can get this. (4-2)+(5-3) = 4. So, you can calculate the entire difference array and use kadane's algo to come at a maximum sum subarray. This subarray will denote the start and end in the original arrays that needs to be replaced.
Will this fail ??
I thought of calculating a difference array, too, at first, but wasn't sure how to use it afterwards. I had forgotten about Kadane's algorithm. I just looked it up and, although I understand the algorithm itself, I didn't fully understand how to use it in this specific problem. Can you explain how it's used in your approach?
So my approach is to create a difference array first like if a1= [1,4,2,6,8] a2= [6,4,8,1,2] On calculating difference array we get Diff = [5,0,6,-5,-6] Now if we run kadanes on this we get subarray (0,2) which is 11. So we conclude we need to replace (0,2) Subarray so a1 becomes [6,4,8,6,8]
This doesn't work if, for example, a1=[-2, -2, -2, -100, 4, 5] and a2=[-1, -1, -1, -10000, 5, 6].
The difference array is [1, 1, 1, -9900, 1, 1], so the optimal subarray to replace is (0,2). The new a1 will be [-1, -1, -1, -100, 4, 5], making the maximum subarray sum 9 by choosing subarray (4,5).
However, by replacing subarray (4,5) we get a1=[-2, -2, -2, -100, 5, 6] with maximum sum 11 by choosing the subarray (4,5).
So it's not enough to choose the subarray of the difference array with the maximum sum.
Got it.. thanks a lot
I am not sure whether this is correct or not, but I think you can use dp and Kadane algorithm
the dp states are i and j where i is the index and j is $$$0$$$ if you haven't taken any element from $$$b$$$, $$$1$$$ if the previous element was from $$$b$$$ (i.e. you can still take element $$$i$$$ from $$$b$$$) and $$$2$$$ if you can no longer take elements from $$$b$$$
$$$dp_{sum}[i][j]$$$ is the sum until state $$$i, j$$$, $$$dp_{max}[i][j]$$$ is the maximum subarray sum till state $$$i, j$$$.
here is my implementation to this:
I tried it on some randomly generated tests and it works