I have watched the video editorial and then i tried approaching the problem, but there seems to be an error. I am almost certain that i implemented the algorithm properly but there seems to be an error which i cannot pinpoint can someone help me out?
i think your operators are flipped by mistake while calculating min_a and max_b
instead you are calculating max_a and min_b
also should also reset loc_a and loc_b to zero, (i am not sure if it will make any difference.)
Don’t know why my solution also gives WA. Can anyone tell which test case it might be failing?
@sedlif i checked the variables min_a and max_b seem correct, however i didnt understand what you tried to say about loc_a and loc_b. I already have initialized them to zero before the start of loop. What more are you asking me to do?
Test Case : -
100 10 5 10 5
100 100 1 2 5
Your Answer is “-1”
But the answer should be “1”
Explanation: - If we swap 100 (first element of 2nd array) with 10 or 5 or 1 then we will get more number of votes for A, with just 1 swap.
So if you need to sort John Jackson’s vote in ascending order and Jack Johnson’s vote in Descending and then compare one by one swapping first element of both arrays.
Time complexity will be O(max(n, m)).
Hope you have understood.
This Is my solution, which seems to be perfect to me - I am getting right outputs still its wrong… Can someone explain this?
here you go, i have commented the differences.
In the first for loop, you are only running if sum(A) is smaller than sum(B). What if you do one swap and they become equal? Will you not do any more swaps and return -1? I think it should work by changing < to <=
A few other comments (ignore if you don’t find useful)
- Don’t use statements like for t in range(t). It can have bad consequences and it’s neater to use different variable names. e.g. for t in range(T).
- Instead of writing, d=max(B), B[ma]=min(A), A[mi]=d in 3 lines. You could go with A[mi],B[ma]=B[ma],A[mi] in a single line
Thank You So much… Also Thanks for the other info… Just one thing I didn’t understood that why using same variable in “for t in range(t)” is a problem?
Maybe it’s just me but when you do that, you might lose track of what does t mean?
t stores the total number of test cases. But now you are resetting it to the ID of the current test case in the scope of the loop. If you actually tried to access the total number of test cases inside the loop you would fail to do that.
As I said, please ignore if it does not sound useful. For me, having clean code means not overriding variables unnecessarily.