C++ Solution Partially correct


#1

The approach is used is :

  1. Take the total sum of digits at odd place and then at the even’s place.

  2. Then iterate over each element of the array again (to remove it ).

  3. Calculate the ODDSUM and EVENSUM of the elements after removing the current element.

  4. Calculate the overall change in the ODDSUM and the EVENSUM.

  5. If = then ans++.

    int Solution::solve(vector &A) {

    if(A.size() == 1){return 1;}
    if(A.size() <=2){return 0;}

    int ans = 0;
    int O = 0, E = 0;

    for(int i=0 ; i<A.size() ; i++){
    if(i&1)
    O+=A[i];
    else
    E+=A[i];
    }

    for(int i=0, OT = O, ET = E ; i<A.size() ; i++, OT = O, ET = E){

     if(i&1){
         OT -= A[i];
     }
     else
         ET -= A[i];
     
     int OA = 0, EA=0;
     
     for(int j=i+1 ; j<A.size() ; j++){
         if(j&1){
             OA += A[j];
         }
         else{
             EA += A[j];
         }
     }
     
     OT = OT + EA - OA;
     ET = ET - EA + OA;
     
     if(OT == ET)
         ans++;
    

    }

    return ans;

}

How can this be optimised more ?


#2

You can use prefix sum array to solve this one.