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 ...
Chef and Operations CodeChef SnackDown 2021 Beginner Practice Contest
Question:
Chef has two sequences and , each with length . He can apply the following magic operation an arbitrary number of times (including zero): choose an index () and add to , to and to , i.e. change to , to and to .
Chef asks you to tell him if it is possible to obtain sequence from sequence this way. Help him!
Input:
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains a single integer .
- The second line contains space-separated integers .
- The third line contains space-separated integers
Output:
For each test case, print a single line containing the string
"TAK"
if it is possible to reach sequence or "NIE"
otherwise.Constraints:
- for each valid
- for each valid
- the sum of for all test cases does not exceed
Example Input:
2
5
0 0 0 0 0
1 2 4 2 3
5
0 0 0 0 0
1 2 4 2 4
Example Output:
TAK
NIE
Explanation:
Example case 1: Chef can apply the operation at index and get the sequence . Afterwards, he can apply that operation at index and get the sequence .
Example case 2: It is impossible to reach sequence .
Program Code in C++
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t;
cin >> t;
while(t--) {
vector<int>v;
int n;
cin >> n;
for(int i = 0; i<n; i++) {
int temp;
cin >> temp;
v.push_back(temp);
}
bool b = true;
int i;
for(i =0; i<n-2; i++) {
int temp;
cin >> temp;
int diff = temp - v[i];
if(diff < 0)
b = false;
v[i] += (1*diff);
v[i+1] += (2*diff);
v[i+2] += (3*diff);
if(v[i] != temp)
b = false;
}
//cout << i <<endl;
int temp;
cin >> temp;
if(v[i] != temp)
b = false;
i++;
cin >> temp;
if(v[i] != temp)
b = false;
if(!b)
cout << "NIE" <<endl;
else
cout << "TAK" <<endl;
}
return 0;
}
Important Links:
More Information
CodeChef is one of the largest online coding platform and SnackDown is one of it's of grandest programs held ever year. SnackDown is a global programming event that invites programmers all over the world to participate in India's most prestigious multi-round programming competition. SnackDown is open to everyone who has a knack in programming.
This question that is Chef and Operations is of CodeChef SnackDown 2021 Beginner Practice Contest. Here you will get a brief explanation of the problem and after reading the explanation if you are still stuck and have no clue to understand the problem then you can visit the solution of the question given above.
Hope you learnt something from this explanation and solution. Code it guys, practice coding more and more.
Comments
Post a Comment