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 test
Dance Moves CodeChef SnackDown 2021 Round 1A Solution
Question
This year Chef is participating in a Dancing competition. The dance performance will be done on a linear stage marked with integral positions. Initially, Chef is present at position X and Chef's dance partner is at position Y. Chef can perform two kinds of dance moves.
If Chef is currently at position k, Chef can:
Moonwalk to position k+2, or
Slide to position k−1
Chef wants to find the minimum number of moves required to reach his partner. Can you help him find this number?
Input Format
First line will contain a single integer T, the number of testcases. Then the description of T testcases follows.
Each testcase contains a single line with two space-separated integers X,Y, representing the initial positions of Chef and his dance partner, respectively.
Output Format
For each testcase, print in a separate line, a single integer, the minimum number of moves required by Chef to reach his dance partner.
Constraints
1≤T≤10^3
−10^3≤X,Y≤10^3
Sample Input 1
4
3 8
-11 -5
57 492
-677 913
Sample Output 1
4
3
219
795
Explanation
Test Case 1: Chef will perform Moonwalk thrice, followed by a slide reaching at position 8. This makes 4 dance steps in total.
Test Case 2: Performing Moonwalk thrice will take Chef to his dance partner.
Program Code in C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
ll t;
cin>>t;
while(t--){
ll x,y,k=0;
cin>>x>>y;
while(x!=y){
if(x>y){
x-=1;
k++;
}
if(x<y){
x+=2;
k++;
}
}
cout<<k<<endl;
}
return 0;
}
Comments
Post a Comment