Candy Love - WA - What's the defect in my code?

Can anyone please help me to figure out my fault?? Its give WA for which test case(s)??

import sys

def main():
  # sys.stdin = open('in.txt', 'r') 
  # sys.stdout = open('out.txt', 'w')
  t = int(sys.stdin.readline())
  for _ in range(t):
    n = int(sys.stdin.readline())
    c = list(map(int, sys.stdin.readline().split()))
    x = int(sys.stdin.readline())
    if n == 1:
      print(1, 0)
      continue

    e1, e2, b1, b2, i = c[0], c[n - 1], 1, 1, 1
    while b1 + b2 < n:
      if e1 < x * e2:
        e1 += c[i]
        b1 += 1
        i += 1
      elif e1 > x * e2:
        b2 += 1
        e2 += c[n - b2]
      else: #e1 == x * e2:
        if b1 >= b2:
          e1 += c[i]
          i += 1
          b1 += 1
        else: #b1 < b2:
          b2 += 1
          e2 += c[n - b2]

    print(b1, b2)

if __name__ == "__main__":
  main()

EDIT :
Solution link [WA]

[N.B.: Some portion of this code changed (but not logically) for clarification.]

[N.B.: Some portion of this code changed (but not logically) for clarification.]

Yeah, no. What you have now works, what you had before does not.

The loop,

for i in range(1, n - 1):

Does not work the way you think it does, when you edit i inside the loop i is treated as a local variable so is reset to what its next should have been rather than the current value of i+1 as you would have wanted.
Use a while loop if you want to work it like this.
Anyway, heres input to give an incorrect answer with the original code

10
2 2 2 2 1 1 1 1 1 1
2
1 Like

Oh teri, I got it now, Thanks for helping :heart:

Can you point what’s wrong with this… i have used ta and tb to store arrival time of A and B… it only passes task 1 of sub-task 2…

#include<bits/stdc++.h>
using namespace std;

int main(){
int t;cin>>t;while(t–){

    long n;cin>>n;float c[n];
    
    for(long i=0;i<n;i++){
        cin>>c[i];
    }
    
    float x;cin>>x;
    float ta[n];
    float tb[n];
    
    ta[0]=0;tb[n-1]=0;
    
    for(long i=1;i<n;i++){
        ta[i]=ta[i-1]+c[i-1]/x;
    }
    for(long i=n-2;i>=0;i--){
        tb[i]=tb[i+1]+c[i+1];
    }
    
    int a=0,b=0;
    for(long i=0;i<n;i++){
        if(ta[i]<=tb[i]){
            a++;
        }
        else{
            b++;
        }

    }

    cout<<a<<" "<<b<<'\n';
}

}