Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: 3
Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9
Sum of elements at odd indices = 2 + 4 + 6 = 12
Difference = 9 -12 = -3
On shifting the subarray from [0, 2] to the end the array becomes {4, 5, 6, 1, 2, 3}
Sum of elements at even indices = 4 + 6 + 2 = 12
Sum of elements at odd indices = 5 + 1 + 3 = 9
Difference = 12 – 9 = 3
This gives the maximum answer out of all such shifts that is equal to 3.
Input: arr[] = {3, 4, 8}
Output: 7
Case 1: When length of the array N is even and l (start of subarray) is even {a,b,c,d,e,f} for l = 2, r =4 (one based indexing)
Initial: +a – b + c – d + e -f on shifting sub array of [2,4] to the end the array becomes ={a, e, f, b, c, d}
Final: + a- e +f – b + c-d
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] remains same and part3 (r, N] becomes negative (opposite sign) after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum1 = prefix_sum(l-1) + ( prefix_sum(r) – prefix_sum(l-1) ) – ((prefix_sum(n) – prefix_sum(r))
sum1 = 2* prefix_sum(r) – prefix_sum(N)
Case 2: When length of the array N is even and l (start of subarray) is odd {a, b, c, d, e, f} for l = 3, r = 5(one based indexing)
Initial: +a – b + c – d + e -f on shifting [3,5] to the end the array becomes = {a, b, f, c, d, e}
Final: + a – b + f – c + d – e
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] becomes opposite sign after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum2 = prefix_sum(l-1) – ( prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum2 = 2* prefix_sum(l-1) – prefix_sum(N)
Case 3: When length of the array N is odd and l (start of subarray) is even {a, b, c, d, e} for l = 2, r = 4 (one based indexing)
Initial: +a – b + c – d + e on shifting sub array of [2,4] to the end the array becomes = {a, e, b, c, d}
Final: + a – e + b – c + d
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] becomes negative(opposite sign) after shifting.
If prefix_sum array is computed then sum after shifting becomes
sum3 = prefix_sum(l-1) – (prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum3 = 2* prefix_sum(l-1) – prefix_sum(N)
Case 4: When length of the array N is odd and l (start of subarray) is odd {a, b, c, d, e} for l = 3, r =3(one based indexing)
Initial: +a – b + c – d + e on shifting [3,5] to the end the array becomes = {a, b, d, e, c}
Final: + a – b + d – e + c
On diving the array to 3 subparts [1, l) , [l, r] , (r, N].
The sign of part1 [1, l) remains same, part2 [l, r] becomes opposite sign and part3 (r, N] remains same.
If prefix_sum array is computed then sum after shifting becomes
sum4 = prefix_sum(l-1) + (prefix_sum(r) – prefix_sum(l-1)) – ((prefix_sum(n) – prefix_sum(r))
sum4 = 2* prefix_sum(r) – prefix_sum(N)
- If n is even result becomes: max(sum1, sum2).
- If n is odd result becomes: max(sum3,sum4).