HackerEarth October Easy 2021 Editorial Solutions
Likeable Arrays
Bob and Alice are two friends, they have an array consisting of integers, . Alice likes the arrays in which if element is present it must have exactly 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 .
- 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 denoting the number of test cases.
- The first line of each test case contains an integer denoting the number of elements in array .
- The second line of each test case contains space-separated integers of array .
Output format
Print lines. For each test case:
- Print a single line indicating the minimum number of operations to be performed.
Constraints
The sum of over all test cases does not exceed 200000.
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++
- #include <bits/stdc++.h>
- using namespace std;
- int solve(vector<int> A){
- int ans=0;
- map<int,int> m;
- for(int i=0;i<A.size();i++){
- if(m.find(A[i])==m.end()) m[A[i]]=1;
- else m[A[i]]++;
- }
- for(auto itr:m){
- if(itr.first==itr.second) continue;
- else{
- if(itr.second>itr.first) ans+=(itr.second-itr.first);
- else ans+=min(itr.second,itr.first-itr.second);
- }
- }
- return ans;
- }
- int main(){
- int t;
- cin>>t;
- while(t--){
- int n;
- cin>>n;
- vector<int> A(n);
- for(int i=0;i<n;i++) cin>>A[i];
- cout<<solve(A)<<endl;
- }
- }
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 denoting the number of test cases.
- The first line of each test case contains an integer denoting the number of strings on the table.
- Next lines of each test case contain the strings present on the table.
Output format
Print lines. For each test case:
- Print a single line indicating the number of good pairs of strings.
Constraints
The sum of length over all test cases does not exceed 500000.
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++
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- int32_t main()
- {
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- }
- int t;
- cin>>t;
- while(t--)
- {
- int n;
- cin>>n;
- string arr[n];
- int all0=0,all1=0;
- for(int i=0;i<n;i++)
- {
- cin>>arr[i];
- int countzero=0;
- for(int j=0;j<arr[i].length();j++)
- {
- if(arr[i][j]=='0')
- {
- countzero++;
- }
- }
- if(countzero==arr[i].length())
- {
- all0++;
- }
- else if(countzero==0)
- {
- all1++;
- }
- }
- int all=all0+all1;
- 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);
- cout<<ans<<endl;
- }
- }
Ternary Palindromes
You are given a ternary string of length consisting of 0, 1, and 2. Your task is to determine the number of distinct permutations of for which satisfies the below condition:
Let be the permutation of , is valid if the count of all palindromic substrings (sizes from 1 to ) does not exceed .
Input format
- The first line contains an integer denoting the number of test cases .
- The first line of each test case contains a ternary string .
Output format
Print 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 .
Constraints
The sum of the length of strings over all test cases does not exceed 200000.
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++
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int t;
- cin >> t;
- vector<string> base{"012", "021", "102", "120", "201", "210"};
- while (t--) {
- string s;
- cin >> s;
- int n = s.size();
- vector<string> perms;
- for (int j = 0; j < 6; ++j) {
- string str;
- for (int i = 0; i < n; ++i) {
- str += base[j];
- }
- str.resize(n);
- perms.push_back(str);
- }
- int res = 0;
- for (int j = 0; j < 6; ++j) {
- int cnt1[3] = {0};
- int cnt2[3] = {0};
- for (int i = 0; i < n; ++i) {
- cnt1[perms[j][i] - '0']++;
- cnt2[s[i] - '0']++;
- }
- if (cnt1[0] == cnt2[0] and cnt1[1] == cnt2[1] and cnt1[2] == cnt2[2]) {
- res++;
- }
- }
- cout << res << '\n';
- }
- }
Deque Sorting
You are given an array of 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 .
- The first line of each test case contains an integer denoting the number of elements in array .
- The second line of each test case contains space-separated integers of array .
Output format
Print lines. For each test case:
- Print a single line indicating the minimum number of operations to be performed.
Constraints
All are distinct.
The sum of N over all test cases does not exceed 200000.
[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++
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- #define pi(x) cout<<x;
- #define ps(x) cout<<x<<" ";
- #define pnl(x) cout<<x<<"\n";
- #define for0(n) for(i=0;i<n;i++)
- #define for1(n) for(i=1;i<=n;i++)
- #define m(x) memset(x,0,sizeof x);
- #define nl cout<<"\n";
- #define mp make_pair
- #define pb push_back
- #define fr first
- #define se second
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- 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;
- cin>>test;
- while(test--){
- cin>>n;
- vector<pair<ll,ll>>a(n);
- for(i=0;i<n;i++){
- cin>>a[i].first;
- a[i].second=i;
- }
- sort(a.begin(),a.end());
- max1=1;
- x1=1;
- for(i=1;i<n;i++){
- if(a[i].second>a[i-1].second){
- x1++;
- }else{
- x1=1;
- }
- max1=max(max1,x1);
- }
- cout<<n-max1<<"\n";
- }
- return 0;
- }

Comments
Post a Comment