RAINBOWA: Chef and Rainbow Array
Link for the Problem: RAINBOWA
Problem:
In the problem, you have to find if the given array is a Rainbow Array or not.
Solution:
Consider the consecutive similar elements as bundles. Each bundle will consist of 2 characteristics. First the number of similar elements in that bundle and the second, value of similar elements in that bundle.
We will handle these bundles by vector pair. It can be done with other methods too. If the size of the vector is not equal to 13 then acc to problem array will be not a Rainbow array. And the vector should be a palindrome.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n],i,j,k;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
vector<pair<int,int>> v;
int c=1,vl=a[0];
for(i=1;i<n;i++)
{
if(a[i]!=a[i-1])
{
v.push_back(make_pair(c,vl));
c=1;
vl=a[i];
}
else
{
c++;
}
}
v.push_back(make_pair(c,vl));
int f=0;
if(v.size()==13&&v[6].second==7)
{
for(i=0;i<6;i++)
{
if(v[i].first!=v[12-i].first||v[i].second!=i+1||v[12-i].second!=i+1)
{
f=1;
break;
}
}
}
else
{
f=1;
}
if(f==0)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
Link for the Problem: RAINBOWA
Problem:
In the problem, you have to find if the given array is a Rainbow Array or not.
Solution:
Consider the consecutive similar elements as bundles. Each bundle will consist of 2 characteristics. First the number of similar elements in that bundle and the second, value of similar elements in that bundle.
We will handle these bundles by vector pair. It can be done with other methods too. If the size of the vector is not equal to 13 then acc to problem array will be not a Rainbow array. And the vector should be a palindrome.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[n],i,j,k;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
vector<pair<int,int>> v;
int c=1,vl=a[0];
for(i=1;i<n;i++)
{
if(a[i]!=a[i-1])
{
v.push_back(make_pair(c,vl));
c=1;
vl=a[i];
}
else
{
c++;
}
}
v.push_back(make_pair(c,vl));
int f=0;
if(v.size()==13&&v[6].second==7)
{
for(i=0;i<6;i++)
{
if(v[i].first!=v[12-i].first||v[i].second!=i+1||v[12-i].second!=i+1)
{
f=1;
break;
}
}
}
else
{
f=1;
}
if(f==0)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
No comments:
Post a Comment