Skip to main content

Equal Beauty CodeChef SnackDown 2021 Round 1A

 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 ...

HackerEarth October Easy 2021 Editorial Solutions

 HackerEarth October Easy 2021 Editorial Solutions 

HackerEarth October Easy 2021 Editorial


Likeable Arrays

Bob and Alice are two friends, they have an array A consisting of N integers, A1,A2,A3 ..., AN. Alice likes the arrays in which if element X is present it must have exactly X or zero occurrences. So, Bob has decided to convert this array to an array which Alice likes. To do that, he can perform the following two operations:

  •         Add an element of any value to array A.
  •         Remove an element from array A.

Find the minimum number of operations Bob has to perform so that array is liked by Alice.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of elements in array A.
  • The second line of each test case contains N space-separated integers of array A.

Output format

Print T lines. For each test case:

  • Print a single line indicating the minimum number of operations to be performed.

Constraints

1T20000

1N200000

1Ai109i[1,N]

The sum of N over all test cases does not exceed 200000.

Sample Input
1
5
3 2 3 1 2
Sample Output
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

According to the defination: 1 should have exactly one or zero occurence, 2 should have two or zero occurences and 3 should have three or zero occurences.

So for three, we have two options, either we remove both occurences or add one occurence, it will be optimal to add one occurence.

Program Code in C++

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int solve(vector<int> A){
  4. int ans=0;
  5. map<int,int> m;
  6. for(int i=0;i<A.size();i++){
  7. if(m.find(A[i])==m.end()) m[A[i]]=1;
  8. else m[A[i]]++;
  9. }
  10. for(auto itr:m){
  11. if(itr.first==itr.second) continue;
  12. else{
  13. if(itr.second>itr.first) ans+=(itr.second-itr.first);
  14. else ans+=min(itr.second,itr.first-itr.second);
  15. }
  16. }
  17. return ans;
  18. }
  19.  
  20. int main(){
  21. int t;
  22. cin>>t;
  23.  
  24. while(t--){
  25. int n;
  26. cin>>n;
  27. vector<int> A(n);
  28. for(int i=0;i<n;i++) cin>>A[i];
  29. cout<<solve(A)<<endl;
  30. }
  31. }

Good Strings

There are N binary strings (consisting of 0s and 1s) in a table. A pair of strings is good if there exists at least one character in common in both strings.
Find the number of good pairs of strings.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of strings on the table.
  • Next N lines of each test case contain the strings present on the table.

Output format

Print T lines. For each test case:

  • Print a single line indicating the number of good pairs of strings.

Constraints

1T10000

1N100000

1Length of String5

The sum of length over all test cases does not exceed 500000.

Sample Input
2
3
11
00
00000
5
01
1111
0001
11
01
Sample Output
1
10
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First Test Case:   String 2 and 3 have character 0 in common

Second test case: All strings have 1 in common so answer is number of pairs, which is 10

Program Code in C++

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. int32_t main()
  5. {
  6.  
  7. {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10. }
  11. int t;
  12. cin>>t;
  13. while(t--)
  14. {
  15. int n;
  16. cin>>n;
  17. string arr[n];
  18. int all0=0,all1=0;
  19. for(int i=0;i<n;i++)
  20. {
  21. cin>>arr[i];
  22. int countzero=0;
  23. for(int j=0;j<arr[i].length();j++)
  24. {
  25. if(arr[i][j]=='0')
  26. {
  27. countzero++;
  28. }
  29. }
  30. if(countzero==arr[i].length())
  31. {
  32. all0++;
  33. }
  34. else if(countzero==0)
  35. {
  36. all1++;
  37. }
  38. }
  39. int all=all0+all1;
  40. int ans=(n*(n-1))/2-(all>=2 ? (all*(all-1))/2 : 0)+(all0>=2 ? (all0*(all0-1))/2 : 0)+(all1>=2 ? (all1*(all1-1))/2 : 0);
  41. cout<<ans<<endl;
  42. }
  43. }

Ternary Palindromes

You are given a ternary string S of length N consisting of 0, 1, and 2. Your task is to determine the number of distinct permutations of S for which satisfies the below condition:

Let R be the permutation of SR is valid if the count of all palindromic substrings (sizes from 1 to N) does not exceed N.

Input format

  • The first line contains an integer denoting the number of test cases T.
  • The first line of each test case contains a ternary string S.

Output format

Print T lines. For each test case:

  • Print a single line indicating the number of ways to rearrange S such that the number of palindromes does not exceed N.

Constraints

1T20000

1N200000

The sum of the length of strings over all test cases does not exceed 200000.

Sample Input
2
201
22011
Sample Output
6
2
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First test case:  All the permutations of this string are different and valid.
"012","021","102","120","210" and "201".

Second test case: Two permutations are valid.
"12012" and "21021"

In each of the above strings there are only 1 length palindromes, which is equal to length of string.

Program Code in C++

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(0);
  6. int t;
  7. cin >> t;
  8. vector<string> base{"012", "021", "102", "120", "201", "210"};
  9. while (t--) {
  10. string s;
  11. cin >> s;
  12. int n = s.size();
  13. vector<string> perms;
  14. for (int j = 0; j < 6; ++j) {
  15. string str;
  16. for (int i = 0; i < n; ++i) {
  17. str += base[j];
  18. }
  19.  
  20. str.resize(n);
  21. perms.push_back(str);
  22. }
  23.  
  24. int res = 0;
  25. for (int j = 0; j < 6; ++j) {
  26. int cnt1[3] = {0};
  27. int cnt2[3] = {0};
  28.  
  29. for (int i = 0; i < n; ++i) {
  30. cnt1[perms[j][i] - '0']++;
  31. cnt2[s[i] - '0']++;
  32. }
  33.  
  34. if (cnt1[0] == cnt2[0] and cnt1[1] == cnt2[1] and cnt1[2] == cnt2[2]) {
  35. res++;
  36. }
  37. }
  38.  
  39. cout << res << '\n';
  40. }
  41. }

Deque Sorting

You are given an array A of N distinct integers, your task is to sort the given array using the below operation.

  •  Choose any element, take it out of the array without changing the order of other elements, and put it back either in front or back.

Your task is to print the minimum number of operations to achieve this.

Input format

  • The first line contains an integer denoting the number of test cases T
  • The first line of each test case contains an integer N denoting the number of elements in array A.
  • The second line of each test case contains N space-separated integers of array A.

Output format

Print T lines. For each test case:

  • Print a single line indicating the minimum number of operations to be performed.

Constraints

1T20000

1N200000

1Ai109i[1,N]

All Ai are distinct.

The sum of N over all test cases does not exceed 200000.

Sample Input
1
5
7 3 1 5 4
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

[7,3,1,5,4] in the beginning, for optimal conversion:

  • Remove 5 and insert in the end  [7,3,1,4,5]
  • Remove 7 and insert in the end  [3,1,4,5,7]
  • Remove 1 and insert in the beginning [1,3,4,5,7]

There is no way which involves lesser than three steps.

Program Code in C++

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long int ll; 
  4. #define pi(x) cout<<x;
  5. #define ps(x) cout<<x<<" ";
  6. #define pnl(x) cout<<x<<"\n";
  7. #define for0(n) for(i=0;i<n;i++)
  8. #define for1(n) for(i=1;i<=n;i++)
  9. #define m(x) memset(x,0,sizeof x);
  10. #define nl cout<<"\n";
  11. #define mp make_pair
  12. #define pb push_back
  13. #define fr first
  14. #define se second
  15. int main(){
  16. ios_base::sync_with_stdio(false);
  17. cin.tie(NULL);
  18. cout.tie(NULL);
  19. ll test,h,p,i,j,xy,flag=0,n,u,count,d,o1=0,o2=0,s,e,l,r,x,y,m,z,max1,x1,y1,k,x2,y2,z1,z2,sum,min1;
  20. cin>>test;
  21. while(test--){
  22. cin>>n;
  23. vector<pair<ll,ll>>a(n);
  24. for(i=0;i<n;i++){
  25. cin>>a[i].first;
  26. a[i].second=i;
  27. }
  28. sort(a.begin(),a.end());
  29. max1=1;
  30. x1=1;
  31. for(i=1;i<n;i++){
  32. if(a[i].second>a[i-1].second){
  33. x1++;
  34. }else{
  35. x1=1;
  36. }
  37. max1=max(max1,x1);
  38. }
  39. cout<<n-max1<<"\n";
  40. }
  41. return 0;
  42. }

Comments

Popular posts from this blog

Snake Procession CodeChef SnackDown 2021 Beginner Practice Contest

 Snake Procession CodeChef SnackDown 2021 Beginner Practice Contest Question: The annual snake festival is upon us, and all the snakes of the kingdom have gathered to participate in the procession. Chef has been tasked with reporting on the procession, and for this he decides to first keep track of all the snakes. When he sees a snake first, it'll be its Head, and hence he will mark a 'H'. The snakes are long, and when he sees the snake finally slither away, he'll mark a 'T' to denote its tail. In the time in between, when the snake is moving past him, or the time between one snake and the next snake, he marks with '.'s. Because the snakes come in a procession, and one by one, a valid report would be something like "..H..T…HTH….T.", or "…", or "HT", whereas "T…H..H.T", "H..T..H", "H..H..T..T" would be invalid reports (See explanations at the bottom). Formally, a snake is represented by a 'H...

Qualifying to Pre-Elimination CodeChef SnackDown 2021 Beginner Practice Contest

 Qualifying to Pre-Elimination  CodeChef SnackDown 2021 Beginner Practice Contest Question: Snackdown 2019 is coming! There are two rounds (round A and round B) after the qualification round. From both of them, teams can qualify to the pre-elimination round. According to the rules, in each of these two rounds, teams are sorted in descending order by their score and each team with a score greater or equal to the score of the team at the  K = 1500 K = 1500 -th place advances to the pre-elimination round (this means it is possible to have more than  K K  qualified teams from each round in the case of one or more ties after the  K K -th place). Today, the organizers ask you to count the number of teams which would qualify for the pre-elimination round from round A for a given value of  K K  (possibly different from  1500 1500 ). They provided the scores of all teams to you; you should ensure that all teams scoring at least as many points as the...

Chef and Typing CodeChef SnackDown 2021 Beginner Practice Contest

 Chef and Typing CodeChef SnackDown 2021 Beginner Practice Contest Question: Chef is practising his typing skills since his current typing speed is very low. He uses a training application that displays some words one by one for Chef to type. When typing a word, Chef takes 0.2 seconds to type the first character; for each other character of this word, he takes 0.2 seconds to type this character if it is written with a different hand than the previous character, or 0.4 seconds if it is written with the same hand. The time taken to type a word is the sum of times taken to type all of its characters. However, if a word has already appeared during practice, Chef can type it in half the time it took him to type this word for the first time. Currently, Chef is practising in easy mode, which only uses words that consists of characters 'd', 'f', 'j' and 'k'. The characters 'd' and 'f' are written using the left hand, while the characters 'j...

Kitchen Timetable CodeChef SnackDown 2021 Beginner Practice Contest Solution

 Kitchen Timetable CodeChef SnackDown 2021 Beginner Practice Contest Solution Question: There are  N  students living in the dormitory of Berland State University. Each of them sometimes wants to use the kitchen, so the head of the dormitory came up with a timetable for kitchen's usage in order to avoid the conflicts: The first student starts to use the kitchen at the time  0  and should finish the cooking not later than at the time  A 1 . The second student starts to use the kitchen at the time  A 1  and should finish the cooking not later than at the time  A 2 . And so on. The  N -th student starts to use the kitchen at the time  A N-1  and should finish the cooking not later than at the time  A N The holidays in Berland are approaching, so today each of these  N  students wants to cook some pancakes. The  i -th student needs  B i  units of time to cook. The students have understood that probably...

Chef and Operations CodeChef SnackDown 2021 Beginner Practice Contest

Chef and Operations CodeChef SnackDown 2021 Beginner Practice Contest Question: Chef has two sequences  A A  and  B B , each with length  N N . He can apply the following magic operation an arbitrary number of times (including zero): choose an index  i i  ( 1 ≤ i ≤ N − 2 1 ≤ i ≤ N − 2 ) and add  1 1  to  A i A i ,  2 2  to  A i + 1 A i + 1  and  3 3  to  A i + 2 A i + 2 , i.e. change  A i A i  to  A i + 1 A i + 1 ,  A i + 1 A i + 1  to  A i + 1 + 2 A i + 1 + 2  and  A i + 2 A i + 2  to  A i + 2 + 3 A i + 2 + 3 . Chef asks you to tell him if it is possible to obtain sequence  B B  from sequence  A A  this way. Help him! Input: The first line of the input contains a single integer  T  denoting the number of test cases. The description of  T  test cases follows. The first line of each test case contains a single integer...

Round Robin Ranks CodeChef SnackDown 2021 Round 1A

 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...

Delete Two Elements Educational Codeforces Round 115 Solution

 Delete Two Elements Educational Codeforces Round 115 Solution Introduction Educational Codeforces rounds are organized by Codeforces for the Division 2 Coders. Similarly Educational Codeforces Round was held on     Sunday, October 10, 2021 at 14:35 UTC+5.5   Series of Educational Rounds continue to be held as Harbour Space University initiative. This round will be rated for coders with rating upto 2100. It will be held on extended ICPC rules. The penalty for each incorrect submission until the full correct solution is 10 minutes. After the end of the contest you will have 12 hours to hack any solution. You will be given 7 problems and two hours to solve them.  The contest was a success and lots of submissions were made by the hard striving coders. Problem A that is the Computer Game was very easy and had the maximum number of successful submission. Problem B was a not easy but not difficult also it was fit for an average coder to solve. Problem C although ...

Temple Land CodeChef SnacKDown 2021 Beginner Practice Contest

 Temple Land CodeChef SnacKDown 2021 Beginner Practice Contest Question: The snakes want to build a temple for Lord Cobra. There are multiple strips of land that they are looking at, but not all of them are suitable. They need the strip of land to resemble a coiled Cobra. You need to find out which strips do so. Formally, every strip of land, has a length. Suppose the length of the i-th strip is is  N i , then there will be  N i  integers,  H i1 , H i2 , .. H iN i , which represent the heights of the ground at various parts of the strip, in sequential order. That is, the strip has been divided into  N i  parts and the height of each part is given. This strip is valid, if and only if all these conditions are satisfied: There should be an unique 'centre' part. This is where the actual temple will be built. By centre, we mean that there should be an equal number of parts to the left of this part, and to the right of this part. H i1  = 1 The heights k...

Dance Moves CodeChef SnackDown 2021 Round 1A Solution

 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^...

Unique Email Addresses LeetCode Solution

Unique Email Addresses LeetCode Solution   Question Every  valid email  consists of a  local name  and a  domain name , separated by the  '@'  sign. Besides lowercase letters, the email may contain one or more  '.'  or  '+' . For example, in  "alice@leetcode.com" ,  "alice"  is the  local name , and  "leetcode.com"  is the  domain name . If you add periods  '.'  between some characters in the  local name  part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule  does not apply  to  domain names . For example,  "alice.z@leetcode.com"  and  "alicez@leetcode.com"  forward to the same email address. If you add a plus  '+'  in the  local name , everything after the first plus sign  will be ignored . This allows certain emails to be filtered. Note that...