# FAIRELCT - Answer is wrong, even After getting right output

Below is my code for the Question in January Long Challenge (FAIRELCT - CODE) FAIR ELECTIONS, Even after getting right output for all the cases my answer is not accepted, Can anyone please help me out here. Thank You

``````t=int(input())
for t in range (t):
n,m = [int(i) for i in input().split()]
A = list(int(i) for i in input().split())[:n]
B = list(int(i) for i in input().split())[:m]
C=min(len(A),len(B))
count=0
for x in range(C):
if sum(A)<sum(B):
count=count+1
mi=A.index(min(A))
ma=B.index(max(B))
d=max(B)
B[ma]=min(A)
A[mi]=d
if sum(A)<=sum(B):
print("-1")
else:
print(count)
``````

Please format your code before posting .

2 Likes

Done formatting, can you please help me out with the question?

You can include an if condition to check before iteration itself if the sum of votes for A is greater than that for B. if so, just continue to the next test case. And i think the code will make more sense if we use a while loop. Hereâ€™s my full AC code:

``````t = int(input())

for _ in range(t):
n, m = map(int,input().split())
if sum_a > sum_b:
print(0)
continue

swaps = 0
flag = False
while(sum_a <= sum_b):
sum_a += diff
sum_b -= diff
swaps += 1
else:
flag = True
break
if flag == True:
print(-1)
else:
print(swaps)
``````

Itâ€™s Ok but I want the error in my code because itâ€™s working fine with any inputsâ€¦ Can you give me an example where my code will not work?

I just copy pasted your code and added <= in the if condition of the last for loop and it got AC.
PS : I donâ€™t understand Python Lol

1 Like

https://www.codechef.com/viewsolution/41611492
Hereâ€™s the code

The above is your code. It prints -1 if both arrays have equal sum. Take this test case for example:
1
2 2
5 5
6 4
The correct answer is 1 swap, but your code prints -1.
So as @whitemagic did, you have to convert the sign in the first if statement within the for loop from â€ś<â€ť to â€ś<=â€ť

1 Like

oh man, how i skipped that oneâ€¦ Thank you So much

1 Like

Thank You so much @veeru_uppin Now i understood whatâ€™s wrongâ€¦

1 Like