CodechefMay 02, 2025

Flip the cards

Hazrat Ali

Codechef

There are NN cards on a table, out of which XX cards are face-up and the remaining are face-down.

In one operation, we can do the following:

  • Select any one card and flip it (i.e. if it was initially face-up, after the operation, it will be face-down and vice versa)

What is the minimum number of operations we must perform so that all the cards face in the same direction (i.e. either all are face-up or all are face-down)?

Input Format

  • The first line contains a single integer TT — the number of test cases. Then the test cases follow.
  • The first and only line of each test case contains two space-separated integers NN and XX — the total number of cards and the number of cards which are initially face-up.

Output Format

For each test case, output the minimum number of cards you must flip so that all the cards face in the same direction.

Constraints

  • 1≤T≤5000
  • 2≤N≤100
  • 0≤X≤N

Sample 1:

Input
4
5 0
4 2
3 3
10 2
 
Output
0
2
0
2

Solution
for _ in range(int(input())):
    x,y=map(int, input().split(' '))
    if x==y or y==0:
        print('0')
    elif x-y > int(x/2):
        print(y)
    else:
        print(x-y)



 

Comments