Codechef starters 89 division 3 (rated) Positive or Negative Subarrays
Problem Link : https://www.codechef.com/problems/POSITNEG?tab=statement
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 b[n+1] ;
- for ( int i = 1 ; i <= n ; i++) cin >> b[i] ;
- long long x = 0 , y = 0 ;
- for ( int i = 1 ; i <= n ; i++) {
- if ( b[i] == - 1) x+=i ;
- else y+=i ;
- }
- cout << abs (x - y) << endl;
- }
- }
No comments