Simple DP Approach


#1

class pair
{
public int odd;
public int even;
pair(int o,int e)
{
this.odd=o;
this.even=e;
}
}
public class Solution {
public int solve(int[] A) {
//int esum=0,osum=0;
int count=0;
if(A.length==1)
return 1;
pair[] pairL=new pair[A.length];
pair[] pairR=new pair[A.length];

    int o=0,e=0;
    for(int i=0;i<A.length;i++)
    {
        if(i%2==0)
        {
            e+=A[i];
           pairL[i]=new pair(o,e);
        }
        else
        {
            o+=A[i];
            pairL[i]=new pair(o,e);
        }
    }
    o=0;e=0;
      for(int i=A.length-1;i>=0;i--)
    {   
        if(i%2==0)
        {
            e+=A[i];
            pairR[i]=new pair(o,e);
           
        }
        else
        {
            o+=A[i];
            pairR[i]=new pair(o,e);
        }
    }
    
    for(int i=0;i<A.length;i++)
    {

        int osum=0,esum=0;
        
        if(i==0)
        {
            if(pairR[i+1].odd==pairR[i+1].even)
            count++;
        }
        else if(i==A.length-1)
        {
            if(pairL[i-1].odd==pairL[i-1].even)
            count++;
        }
        else
        {
            if(pairL[i-1].odd+pairR[i+1].even==pairL[i-1].even+pairR[i+1].odd)
            count++;
        }     
    }

    return count;
}

}