EQULDTBN - Editorial

PROBLEM LINK: Div-3 Contest

Author: Bhagya , Rishika
Tester: Rishika
Editorialist: Bhagya

DIFFICULTY:

EASY

PREREQUISITES:

Strings and Arrays

PROBLEM:

Equal distribution of candies in such a way that the number of candies taken or returned in minimum.

QUICK EXPLANATION:

Nishu wants to distribute the x candies given by his mom amongst his n friends(including himself). He wants to distribute the candies equally for which he can either ask her mom for more candies or can return her extra candies.

EXPLANATION:

Nishu scored good marks in his previous test, so his mom gave him x candies. Now, he wants to distribute the candies amongst his n friends(including himself). He wants to distribute the candies equally for which he can either ask her mom for more candies or can return her extra candies.

He will do this in such a way that the number of candies that he takes or returns to his mom is minimum. Can you help him?

Constraints

1 <= T <= 10^5
1 <= x,n <=10^9

SOLUTIONS:

import java.util.*;
public class Main {
    public static void main (String args[]) {
 
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
 
        while(t-- > 0)
        {
            int can = sc.nextInt();
            int fr = sc.nextInt();
 
            int n = can / fr;
            int op1 = Math.abs(((n + 1 )* fr) - can);
            int op2 = Math.abs((n * fr ) - can);
 
            System.out.println(Math.min(op1, op2));
        }
    }
}