Header Ads Widget

Ali and Helping innocent people || hackerearth solution

   Problem:-

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

Input Format

The first line contains a string of length 9. The format is "DDXDDD-DD", where D stands for a digit (non zero) and X is an uppercase english letter.

Output Format

Print "valid" (without quotes) if the tag is valid, print "invalid" otherwise (without quotes)

Sample Input
12X345-67
Sample Output
invalid
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The tag is invalid because the sum of first and second digit of it is odd (also the sum of 4'th and 5'th, 5'th and 6'th and 8'th and 9'th are odd).

Code:-

  Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also

Solution 1 ( C language):-

#include<stdio.h>
int main()
{
char s[9];
int i,x;
scanf("%s",s);
int flag1=0,flag2=0;
for(int i=0;i<9;i++)
{
if((s[0]+s[1])%2==0 && (s[3]+s[4])%2==0 && (s[4]+s[5])%2==0 && (s[7]+s[8])%2==0)
flag1=1;
if(s[2]=='A'|| s[2]=='E'|| s[2]=='I'|| s[2]=='O'|| s[2]=='U'|| s[2]=='Y')
flag2=1;
}
if(flag1==1 && flag2==0)
printf("valid");
else
printf("invalid");
return 0;
}

Solution 2 ( C++ language):-

 This solution is based on the c++ language and you can submit ib c++14 and c++17 also.

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int i,x;
cin>>s;
int flag1=0,flag2=0;
for(int i=0;i<9;i++)
{
if((s[0]+s[1])%2==0 && (s[3]+s[4])%2==0 && (s[4]+s[5])%2==0
&& (s[7]+s[8])%2==0)
flag1=1;
if(s[2]=='A'|| s[2]=='E'|| s[2]=='I'|| s[2]=='O'|| s[2]=='U'|| s[2]=='Y')
flag2=1;
}
if(flag1==1 && flag2==0)
cout<<"valid";
else
cout<<"invalid";
return 0;
}

Recommended Post:-



Post a Comment

0 Comments