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 :slight_smile: .

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())
    votes_a = list(map(int,input().split()))
    votes_b = list(map(int,input().split()))
    sum_a = sum(votes_a)
    sum_b = sum(votes_b)
    if sum_a > sum_b:
        print(0)
        continue

    swaps = 0
    flag = False
    while(sum_a <= sum_b):
        i = votes_a.index(min(votes_a))
        j = votes_b.index(max(votes_b))
        if(votes_a[i]<votes_b[j]):
            votes_a[i],votes_b[j] = votes_b[j],votes_a[i]
            diff = abs(votes_a[i]-votes_b[j])
            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

Welcome @adarsh1211