Why all the test cases are not passing?
It fails because the logic is incorrect. This part
else{
ka = ca+k;
kb = cb+k;
t = max(ka,kb);
cout<< t;
}
you cannot assume this. Instead you will need to maintain a window size. Try solving the question using 2 pointers.