-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlipTheCards.java
More file actions
56 lines (41 loc) · 1.35 KB
/
FlipTheCards.java
File metadata and controls
56 lines (41 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Flip the cards
There are N cards on a table, out of which X 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 T — the number of test cases. Then the test cases follow.
The first and only line of each test case contains two space-separated integers N and X — 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
*/
import java.util.*;
public class FlipTheCards {
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
System.out.println((x - y) > y ? y : x - y);
}
}
}