Codeforces Round 909 (Div. 3) 1899C - Yarik and Array Solution


  

Problem Link:   https://codeforces.com/problemset/problem/1899/C

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int t ; cin >> t ;
  8. while(t--) {
  9. int n ; cin >> n ;
  10. int a[n] ;
  11. for ( int i = 0 ; i < n ; i++){
  12. cin >> a[i] ;
  13. }
  14. int current_sum = a[0] ;
  15. int subarray_mx = a[0] ;
  16. for (int i = 1; i < n ; i++) {
  17. if(current_sum < 0 ) current_sum = 0 ;
  18. if ( abs ( a[i] % 2 ) != abs ( a[i - 1] % 2 ) ) {
  19. current_sum += a[i] ;
  20. }
  21. else {
  22. current_sum = a[i] ;
  23. }
  24. subarray_mx = max(subarray_mx , current_sum) ;
  25. }
  26. cout << subarray_mx << endl;
  27. }
  28. return 0 ;
  29. }

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.