Advertisement
Josif_tepe

Untitled

May 26th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. vector<int> pref_sum;
  6. int n;
  7. int suma(int i, int j) {
  8.     if(j < 0 or j >= n) {
  9.         return 0;
  10.     }
  11.     if(i - 1 < 0) {
  12.         return pref_sum[j];
  13.     }
  14.     return pref_sum[j] - pref_sum[i - 1];
  15. }
  16. int main() {
  17.     cin >> n;
  18.     int niza[n];
  19.     for(int i = 0; i < n; i++) {
  20.         cin >> niza[i];
  21.     }
  22.     int sum = 0;
  23.     for(int i = 0; i < n; i++) {
  24.         sum += niza[i];
  25.         pref_sum.push_back(sum);
  26.     }
  27.     for(int i = 0; i < n; i++) {
  28.         int S = 0;
  29.         int E = i - 1;
  30.         int sum_left = suma(S, E);
  31.        
  32.         S = i + 1;
  33.         E = n - 1;
  34.         int sum_right = suma(S, E);
  35.        
  36.         if(sum_left == sum_right) {
  37.             cout << i + 1 << endl;
  38.             return 0;
  39.         }
  40.        
  41.     }
  42.     cout << -1 << endl;
  43.     return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement