Advertisement
Josif_tepe

Untitled

Apr 12th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7. int a[n];
  8. for(int i = 0; i < n; i++) {
  9. cin >> a[i];
  10. }
  11. int L = 0;
  12. int R = n - 1;
  13. int number_of_merges = 0;
  14. while(L <= R) {
  15. if(a[L] == a[R]) {
  16. L++;
  17. R--;
  18. }
  19. else if(a[L] < a[R]) {
  20. L++;
  21. a[L] += a[L - 1];
  22. number_of_merges++;
  23. }
  24. else {
  25. R--;
  26. a[R] += a[R + 1];
  27. number_of_merges++;
  28. }
  29. }
  30. cout << number_of_merges << endl;
  31. return 0;
  32. }
  33.  
  34.  
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement