|
| 1 | +/** |
| 2 | + * Title : Arrays - DS |
| 3 | + * Author : Tridib Samanta |
| 4 | + * Created : 05-06-2020 |
| 5 | + * Link : https://www.hackerrank.com/challenges/arrays-ds/problem |
| 6 | + **/ |
| 7 | + |
| 8 | +#include <bits/stdc++.h> |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +vector<string> split_string(string); |
| 13 | + |
| 14 | +// Complete the reverseArray function below. |
| 15 | +vector<int> reverseArray(vector<int> a) { |
| 16 | + |
| 17 | + vector <int> rev_a; |
| 18 | + |
| 19 | + vector<int> :: reverse_iterator it; |
| 20 | + for (it = a.rbegin(); it != a.rend(); ++it) |
| 21 | + rev_a.emplace_back(*it); |
| 22 | + |
| 23 | + return rev_a; |
| 24 | +} |
| 25 | + |
| 26 | +int main() |
| 27 | +{ |
| 28 | + ofstream fout(getenv("OUTPUT_PATH")); |
| 29 | + |
| 30 | + int arr_count; |
| 31 | + cin >> arr_count; |
| 32 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 33 | + |
| 34 | + string arr_temp_temp; |
| 35 | + getline(cin, arr_temp_temp); |
| 36 | + |
| 37 | + vector<string> arr_temp = split_string(arr_temp_temp); |
| 38 | + |
| 39 | + vector<int> arr(arr_count); |
| 40 | + |
| 41 | + for (int i = 0; i < arr_count; i++) { |
| 42 | + int arr_item = stoi(arr_temp[i]); |
| 43 | + |
| 44 | + arr[i] = arr_item; |
| 45 | + } |
| 46 | + |
| 47 | + vector<int> res = reverseArray(arr); |
| 48 | + |
| 49 | + for (int i = 0; i < res.size(); i++) { |
| 50 | + fout << res[i]; |
| 51 | + |
| 52 | + if (i != res.size() - 1) { |
| 53 | + fout << " "; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fout << "\n"; |
| 58 | + |
| 59 | + fout.close(); |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +vector<string> split_string(string input_string) { |
| 65 | + string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { |
| 66 | + return x == y and x == ' '; |
| 67 | + }); |
| 68 | + |
| 69 | + input_string.erase(new_end, input_string.end()); |
| 70 | + |
| 71 | + while (input_string[input_string.length() - 1] == ' ') { |
| 72 | + input_string.pop_back(); |
| 73 | + } |
| 74 | + |
| 75 | + vector<string> splits; |
| 76 | + char delimiter = ' '; |
| 77 | + |
| 78 | + size_t i = 0; |
| 79 | + size_t pos = input_string.find(delimiter); |
| 80 | + |
| 81 | + while (pos != string::npos) { |
| 82 | + splits.push_back(input_string.substr(i, pos - i)); |
| 83 | + |
| 84 | + i = pos + 1; |
| 85 | + pos = input_string.find(delimiter, i); |
| 86 | + } |
| 87 | + |
| 88 | + splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); |
| 89 | + |
| 90 | + return splits; |
| 91 | +} |
0 commit comments