What's the logic behind the approach


#1

(leftOdd[i] + rightEven[i] == leftEven[i] + rightOdd[i])
what’s the logic behind this approach
I can’t understand why it’s working
pls help!


#2

Now what i expects from you is sit with pen and paper, after reading each statement, try it with given test case for better understanding.
I tried my best to give you hints in every way possible, cause something i was not able explained in order to reduce the editorial length. please try to connect dots by yourself because this will help you in longer run.
Please feel free to ask.

Now Let’s take an example,
array - 1 2 3 4 5 6 7
Index - 1 2 3 4 5 6 7
Now lets delete 4, new array will be,
array- 1 2 3 5 6 7
Index -1 2 3 4 5 6 // elements on the index has been changed after deleting 4.

After shifting
left_odd_sum=1+3 //leftOdd[4]=1+3
right_even_sum=6 //rightEven[4]=6
left_even_sum=2_ //leftEven[4]=2
right_odd_sum=5+7 //rightOdd[4]=5+7

odd_sum_a=(1+3)+(6)=10 //odd_sum_a=left_odd_sum+right_even_sum;
even_sum_a=(2)+(5+7)=14 //even_sum_a=left_even_sum+right_odd_sum;

Now,Why this addition is correct?

Ans is whenever you delete an element, the position of all the elements that are on right to that deleted element will get shifted to left by 1. i.e elements which are on even position will be now on odd and odd index elements will be now on even rest will remain same.

So this is obvious now that it will work.

**_i.e to generalize, calculate all even and odd index elements sum before and after the element which is going to get deleted, store it in the 4 arrays and then you just to need to add those arrays with above mentioned way.

So i am hoping till now you have got the basic idea/intuition of what could be stored in those arrays,(Try to implement before you go down) if not then i am going to explain from here onwards.

So the idea is, every i-th element is considered for the deletion and then below written 2 statements are followed.

we store the sum of odd and even index elements which are on the right and left side of the element (which is going to be deleted) in the rightOdd and leftEven array respectively.

and store the sum of odd and even index elements which are on the left and right side of the element (which is going to be deleted) in the leftOdd and rightEven array respectively.

Eg.
all array will look like

Note:- on each index we are filling what is the sum of odd/even index element before/after the element which is going to be deleted.
That is why some sum are repeating.

These two arrays are for odd index elements sum.
leftOdd[7]={0,0,1,4,4,9,9} leftOdd[1]=0 because there are no number before element 1.
rightEven[7]={12,10,10,6,6,0,0}

These two arrays are for even index elements sum.
leftEven[7]={0,0,2,2,6,6,12}
RightOdd[7]={15,15,12,12,7,7,0}

if i delete 4
leftOdd[4]=1+3 + rightEven[4]=6 == leftEven[4]=2 + rightOdd[4]=5+7
what is the sum of the odd index element sum == what is the sum of the even index element sum