HackerEarth's basic programming solutions( VC Pairs, Arithmetic Progression, Database ) :

Problem 18: VC Pairs

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<iostream>
using namespace std;
int main()
{
int t,n;
cin>>t;
while(t--)
{
string s;
cin>>n;
cin>>s;
int flag=0;
for(int i=0;i<n;i++)
{
if((s[i]!='a'&& s[i]!='e'&& s[i]!='i' && s[i]!='o'&& s[i]!='u') &&
(s[i+1]=='a'|| s[i+1]=='e'|| s[i+1]=='i'|| s[i+1]=='o'|| s[i+1]=='u'))
{
flag++;
}
}
cout<<flag<<"\n";
}
}

I know you are wondering what's this big crap written inside the if parenthesis. Well let me explain. 

&& --> is logical AND . In case of AND all the conditions must be TRUE to make the overall expression TRUE.

|| --> is logical OR . In case of OR only one of the condition must be TRUE to make the overall expression TRUE.

The expression inside the if parenthesis can be broken down into 3 parts -

1. (s[i]!='a' && s[i]!='e' && s[i]!='i' && s[i]!='o'&& s[i]!='u') , here each and every condition is     checked and this block of code returns TRUE only if all the conditions inside are TRUE.

2. && , this means we have to check the next block also.

3. (s[i+1]=='a'|| s[i+1]=='e'|| s[i+1]=='i'|| s[i+1]=='o'|| s[i+1]=='u') , here checking starts from left to right. If the computer encounters a TRUE expression , the whole block returns TRUE , since we used OR there's no need to check the other remaining conditions. 

The if block will get executed only when both the blocks returns TRUE since there's AND between them. 

 

 

 

 

Problem 19: Arithmetic Progression

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int t,a,b,c;
    cin>>t;
    while(t--)
    {
cin>>a>>b>>c;
     cout<<(abs((b-a)-(c-b))+1)/2<<endl;
    }
}

You will be wondering right now , how this solution strike in my mind. Well , i will say it's all about observation. The sample input and output given are -

      input                    output

-5   7    6        7
-10 -100  20       105
 1  -1    1        2
 51  23   10       8

Let's take the example 1 i.e a = -5 , b = 7 and c = 6 , now if we see b-a is not equal to c-b and we have to tell the no. of operations to do so that they follow the rules of arithmatic progression. 

Now let's d = difference which we want to reduce so that b-a = c-b therefore d = (b-a)-(c-b) 

Calculating d in example 1: d = (7+5)-(6-7) = 12+1= 13 and the answer is 7

Calculating d in example 2: d = (-100+10)-(20+100) = -90-120 = 210 and the answer is 105

Calculating d in example 3: d = (-1-1)-(1+1) = -2-2 = -4 and the answer is 2

Therefore , we can say that , the answer relation with d is: answer = abs(d+1)/2 

 

 

 

Problem 20: Database

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<iostream>
using namespace std;
bool is_int(string val) 
{
int len = val.length();
for(int i = 0; i < len; i++) 
 {
if(val[i] < '0' || val[i] > '9') 
 return false;
}
return true;
}
void borer(int A[], int len) {
cout<<"+";
for(int i = 0; i < len; i++) {
for(int j = 0; j < A[i] + 2; j++) {
cout<<"-";
}
cout<<"+";
} cout<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
int tc;
cin>>tc;
while(tc--) {
int m, n;
cin>>m>>n;
string Header[m];
int attr_len[m];
int max_cell[m];
for(int i = 0; i < m; i++) {
cin>>Header[i];
attr_len[i] = Header[i].length();
max_cell[i] = attr_len[i];
}
string data[n][m];
int data_len[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin>>data[i][j];
data_len[i][j] = data[i][j].length();
if(max_cell[j] < data_len[i][j]) max_cell[j] = data_len[i][j];
}
}
//Header
borer(max_cell, m);
cout<<"|";
for(int i = 0; i < m; i++) {
cout<<" "<<Header[i];
for(int j = 0; j < max_cell[i] - attr_len[i] + 1; j++) {
cout<<" ";
} cout<<"|";
} cout<<endl;
borer(max_cell, m);
//data
for(int i = 0; i < n; i++) {
cout<<"|";
for(int j = 0; j < m; j++) {
if(is_int(data[i][j])) {
for(int k = 0; k < max_cell[j] - data_len[i][j] + 1; k++) {
cout<<" ";
}
cout<<data[i][j]<<" ";
} else {
cout<<" "<<data[i][j];
for(int k = 0; k < max_cell[j] - data_len[i][j] + 1; k++) {
cout<<" ";
}
}
cout<<"|";
} cout<<endl;
}
borer(max_cell, m);
}
return 0;
}

This is really a tough problem. When i opened this problem , the solution was already there from HackerEarth .
 

 

 

Guys , if you have any queries related to a question or need more explanation, comment down below! 

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :