Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HackerRank solutions for compare the triplets #109

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions Hackerrank/compare-the-triplets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

// Complete the compareTriplets function below.
vector<int> compareTriplets(vector<int> a, vector<int> b) {
vector<int> c = {0,0};

for(int i = 0; i < a.size(); i++) {
if(a[i] > b[i]) {
c[0] += 1;
}
else if (b[i] > a[i]){
c[1] += 1;
}
}

return c;
}

int main()
{
ofstream fout(getenv("OUTPUT_PATH"));

string a_temp_temp;
getline(cin, a_temp_temp);

vector<string> a_temp = split(rtrim(a_temp_temp));

vector<int> a(3);

for (int i = 0; i < 3; i++) {
int a_item = stoi(a_temp[i]);

a[i] = a_item;
}

string b_temp_temp;
getline(cin, b_temp_temp);

vector<string> b_temp = split(rtrim(b_temp_temp));

vector<int> b(3);

for (int i = 0; i < 3; i++) {
int b_item = stoi(b_temp[i]);

b[i] = b_item;
}

vector<int> result = compareTriplets(a, b);

for (int i = 0; i < result.size(); i++) {
fout << result[i];

if (i != result.size() - 1) {
fout << " ";
}
}

fout << "\n";

fout.close();

return 0;
}

string ltrim(const string &str) {
string s(str);

s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);

return s;
}

string rtrim(const string &str) {
string s(str);

s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);

return s;
}

vector<string> split(const string &str) {
vector<string> tokens;

string::size_type start = 0;
string::size_type end = 0;

while ((end = str.find(" ", start)) != string::npos) {
tokens.push_back(str.substr(start, end - start));

start = end + 1;
}

tokens.push_back(str.substr(start));

return tokens;
}