Header Ad

HackerEarth Ali and Helping innocent people problem solution

In this HackerEarth Ali and Helping innocent people problem solution Arpasland has been surrounded by attackers. A truck enters the city. The driver claims the load is food and medicine from Iranians. Ali is one of the soldiers in Arpasland. He doubts about the truck, maybe it's from the siege. He knows that a tag is valid if the sum of every two consecutive digits of it is even and its letter is not a vowel. Determine if the tag of the truck is valid or not.

We consider the letters "A","E","I","O","U","Y" to be vowels for this problem.


hackerEarth Ali and Helping innocent people problem solution

HackerEarth Ali and Helping innocent people problem solution.

#include <bits/stdc++.h>
using namespace std;

const int maxn = 3e5 + 17;
string s, vowel = "AEOUIY";
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> s;
bool ok = 1;
for(auto i : {0, 3, 4, 7})
ok &= (s[i] + s[i + 1]) % 2 == 0;
ok &= vowel.find(s[2]) == string::npos;
cout << (ok ? "valid" : "invalid") << '\n';
}

second solution

#include<bits/stdc++.h>

using namespace std;

typedef complex<double> base;
typedef long double ld;
typedef long long ll;

#define pb push_back
#define pii pair<int,int>
#define pll pair< ll , ll >
#define vi vector<int>
#define vvi vector< vi >

const int maxn=(int)(1e5+5);
const ll mod=(ll)(1e9+7);
int a[maxn];

bool check(int pos,char ch)
{
if((pos>=0 && pos<=1) || (pos>=3 && pos<=5) || (pos>=7 && pos<=8))
{
return (ch>='0' && ch<='9');
}

if(pos==2)
{
return ch>='A' && ch<='Z';
}

if(pos==6)
{
return ch=='-';
}
}

bool vowel(char ch)
{

if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' || ch=='Y')
{
return true;
}

return false;
}

int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);

string s;cin>>s;

bool ans=true;

for(int i=0;i<s.size();i++)
{
assert(check(i,s[i]));

if(i==2 && vowel(s[i]))
{
ans=false;
}

if(i>=1 && s[i]>='0' && s[i]<='9' && s[i-1]>='0' && s[i-1]<='9')
{
int curr=(s[i]-'0')+(s[i-1]-'0');

if(curr%2==1)
{
cout<<i<<endl;

ans=false;
}
}
}

cout<<(ans?"valid":"invalid")<<endl;

return 0;
}


Post a Comment

0 Comments