DEV Community

Lakshya Tyagi
Lakshya Tyagi

Posted on

JavaScript Code Daily Challenge #9

About

This is a series of JavaScript Code Daily Challenge. Each day I show a few solutions written in JavaScript. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

Minimum and Maximum
https://www.codechef.com/JULY19B/problems/MMAX/
Write Code in Comment

Subtasks

  1. Subtask #1 (30 points): 2≤N,K≤1,000
  2. Subtask #2 (70 points): original constraints

Example Input

1
3
2

Example Output

2

Explanation

Example case 1: To minimise S1, Chef could give 1 chocolate to person 1 and 1 chocolate to person 2, so S1=|1−1|+|1−0|=1.

To maximise S2, Chef can give 1 chocolate to person 1 and 1 chocolate to person 3, since the sequence B=(1,0,1) is a permutation of A=(1,1,0). Then, S2=|1−0|+|0−1|=2.

Top comments (1)

Collapse
 
lakshyatyagi24 profile image
Lakshya Tyagi

It's a JavaScript Series but this time i tried a python code for it

t=int(input())
for i in range(0,t):
    n=int(input())
    k=int(input())
    if(n==2):
        print(1)
    elif(k%n==0):
        print(0)
    else:
        r=k%n
        if(r==(n-r)):
            print(n-1)
        elif(r<(n-r)):
            print(2*r)
        else:
            print(2*(n-r))
Enter fullscreen mode Exit fullscreen mode