Codeforces Round 909 (Div. 3) 1899C - Yarik and Array Solution
Problem Link: https://codeforces.com/problemset/problem/1899/C
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t ; cin >> t ;
- while(t--) {
- int n ; cin >> n ;
- int a[n] ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- int current_sum = a[0] ;
- int subarray_mx = a[0] ;
- for (int i = 1; i < n ; i++) {
- if(current_sum < 0 ) current_sum = 0 ;
- if ( abs ( a[i] % 2 ) != abs ( a[i - 1] % 2 ) ) {
- current_sum += a[i] ;
- }
- else {
- current_sum = a[i] ;
- }
- subarray_mx = max(subarray_mx , current_sum) ;
- }
- cout << subarray_mx << endl;
- }
- return 0 ;
- }
No comments