Advertisement
VergeoPaw

Vergeo Valentino Gunawan 9.A - HackerRank (Diagonal Difference)

Mar 5th, 2021
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7. vector<string> split(const string &);
  8.  
  9. /*
  10.  * Complete the 'diagonalDifference' function below.
  11.  *
  12.  * The function is expected to return an INTEGER.
  13.  * The function accepts 2D_INTEGER_ARRAY arr as parameter.
  14.  */
  15.  
  16. int diagonalDifference(vector<vector<int>> arr,int count) {
  17.     long sum1 = 0;
  18.     long sum2 = 0;
  19.     for (int i=0;i<=count-1;i++) {
  20.         sum1 = sum1 + arr[i][i];
  21.     }
  22.     for (int j=0;j<count;j++) {
  23.         sum2 = sum2 + arr[j][count-j-1];
  24.     }
  25.     long dif;
  26.     if (sum1 >= sum2) {
  27.         dif = sum1 - sum2;
  28.     } else{
  29.         dif = sum2 - sum1;
  30.     }
  31.     return dif;
  32. }
  33.  
  34. int main()
  35. {
  36.     ofstream fout(getenv("OUTPUT_PATH"));
  37.  
  38.     string n_temp;
  39.     getline(cin, n_temp);
  40.  
  41.     int n = stoi(ltrim(rtrim(n_temp)));
  42.  
  43.     vector<vector<int>> arr(n);
  44.  
  45.     for (int i = 0; i < n; i++) {
  46.         arr[i].resize(n);
  47.  
  48.         string arr_row_temp_temp;
  49.         getline(cin, arr_row_temp_temp);
  50.  
  51.         vector<string> arr_row_temp = split(rtrim(arr_row_temp_temp));
  52.  
  53.         for (int j = 0; j < n; j++) {
  54.             int arr_row_item = stoi(arr_row_temp[j]);
  55.  
  56.             arr[i][j] = arr_row_item;
  57.         }
  58.     }
  59.  
  60.     int result = diagonalDifference(arr,n);
  61.  
  62.     fout << result << "\n";
  63.  
  64.     fout.close();
  65.  
  66.     return 0;
  67. }
  68.  
  69. string ltrim(const string &str) {
  70.     string s(str);
  71.  
  72.     s.erase(
  73.         s.begin(),
  74.         find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  75.     );
  76.  
  77.     return s;
  78. }
  79.  
  80. string rtrim(const string &str) {
  81.     string s(str);
  82.  
  83.     s.erase(
  84.         find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  85.         s.end()
  86.     );
  87.  
  88.     return s;
  89. }
  90.  
  91. vector<string> split(const string &str) {
  92.     vector<string> tokens;
  93.  
  94.     string::size_type start = 0;
  95.     string::size_type end = 0;
  96.  
  97.     while ((end = str.find(" ", start)) != string::npos) {
  98.         tokens.push_back(str.substr(start, end - start));
  99.  
  100.         start = end + 1;
  101.     }
  102.  
  103.     tokens.push_back(str.substr(start));
  104.  
  105.     return tokens;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement