Equal Beauty CodeChef SnackDown 2021 Round 1A Question The beauty of an (non-empty) array of integers is defined as the difference between its largest and smallest element. For example, the beauty of the array [2,3,4,4,6] is 6−2=4. An array A is said to be good if it is possible to partition the elements of A into two non-empty arrays B1 and B2 such that B1 and B2 have the same beauty. Each element of array A should be in exactly one array: either in B1 or in B2. For example, the array [6,2,4,4,4] is good because its elements can be partitioned into two arrays B1=[6,4,4] and B2=[2,4], where both B1 and B2 have the same beauty (6−4=4−2=2). You are given an array A of length N. In one move you can: Select an index i (1≤i≤N) and either increase Ai by 1 or decrease Ai by 1. Find the minimum number of moves required to make the array A good. Input Format The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follow. Each ...
Round Robin Ranks CodeChef SnackDown 2021 Round 1A
Question
A round-robin tournament is being held in Chefland among N teams numbered 1,2,...,N. Every team play with all other teams exactly once. All games have only two possible results - win or loss. A win yields 2 points to the winning team while a loss yields no points. What is the maximum number of points a team finishing at the Kth position can score?
Note: If two teams have the same points then the team with the higher team number achieves the better rank.
Input Format
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains a single line of input, two space-separated integers N,K.
Output Format
For each testcase, output in a single line an integer - the maximum points the team ranked K in the round-robin tournament can score.
Constraints
1≤T≤10^5
1≤K≤N≤10^9
Sample Input 1
3
3 3
4 1
7 4
Sample Output 1
2
6
8
Explanation
Test Case 1: There are 3 teams in the tournament. The maximum score will be achieved by the team coming 3rd when all teams win 1 match and lose the other one. Hence the maximum possible score will be 2(2⋅1) points.
Test Case 2: There are 4 teams in the tournament. The maximum score will be achieved by the team coming 1st when they beat all teams in the tournament. Hence the maximum possible score will be 6(2⋅3) points.
Program Code in C++
Here we are just providing the main logic of the question we are not providing the full solution since that is too simple to do. You just need to take the input in long long. Run a while loop and take inputs n and k. Then you just need the logic given below.
answer = (2*n-k-1)/2;
answer*=2;
cout<<answer<<endl;
Can u explain this formula
ReplyDelete++
Delete