CodeChef - Smallest Possible Whole Number
You are given two integers N and K. You may perform the following operation any number of times (including zero): change N to N−K, i.e. subtract K from N. Find the smallest non-negative integer value of N you can obtain this way.
Input
- The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
- The first and only line of each test case contains two space-separated integers N and K.
Output
For each test case, print a single line containing one integer — the smallest value you can get.
Constraints
- 1 ≤ T ≤ 10^5
- 1 ≤ N ≤ 10^9
- 0 ≤ K ≤ 10^9
Subtasks
Subtask #1 (100 points): original constraints
Sample 1:
Input. Output3
5 2
4 4
2 51
0
2
Explanation:
Example case 1:
- First, we change N=5 to N−K=5−2=3.
- Then, we have N=3 and we change it to N−K=3−2=1.
Since 1<K, the process stops here and the smallest value is 11.
Example case 2: We change N=4 to N−K=4−4=0. Since 0<K, the process stops here and the smallest value is 0.
Example case 3: Since 2<K initially, we should not perform any operations and the smallest value is 2.
import java.util.Scanner;
class SmallestWholeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int N = sc.nextInt();
int K = sc.nextInt();
if (K == 0) {
System.out.println(N);
} else {
System.out.println(N % K);
}
}
sc.close();
}
}