Repository navigation
Expand file tree
/
Copy pathC_Yarik_and_Array.cpp
More file actions
153 lines (116 loc) · 3.1 KB
/
Copy pathC_Yarik_and_Array.cpp
File metadata and controls
153 lines (116 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
int t = 1; // Number of test cases
cin >> t;
while (t--) {
ll n; // Length of the array
cin >> n;
vector<ll> v(n); // Array to store elements
// Reading the array elements
for (int i = 0; i < n; i++) { // O(n)
cin >> v[i];
}
ll ans = INT_MIN; // Variable to store the maximum sum of subarray
int i = 0, j = 0; // Pointers for the start and end of the subarray
ll sum = 0; // Current sum of the subarray
// Iterate over the array to find the maximum sum subarray with alternating parities
while (j < n) { // O(n)
// Reset sum if it becomes negative
if (sum < 0) {
sum = 0;
i = j;
}
// Check if the current element can be added to the subarray
if (i < j) {
// Check if the current and previous elements have alternating parities
if ((v[j] ^ v[j - 1]) & 1) {
sum += v[j];
} else {
sum = v[j];
i = j;
}
} else {
sum = v[j];
}
// Update the maximum sum found so far
ans = max(ans, sum);
j++;
}
// Output the maximum sum for the current test case
cout << ans << endl;
}
// Time Complexity (TC): O(n)
// Space Complexity (SC): O(n)
}
/*
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rep(i, a, b) for (int i = a; i < b; i++)
void solve() {
}
int main() {
fastio;
int t;
cin >> t;
while (t--) {
int n;
cin>>n;
vector<pair<int , char>> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i].first;
if(arr[i].first%2!=0) arr[i].second = 'O';
else arr[i].second = 'E';
}
int l = 0 , r = 0;
int sum = arr[0].first ;
int ori = arr[0].first;
while(l<n){
ori = max(ori , sum);
if(r+1<n && arr[r].second != arr[r+1].second){
r++;
sum+=arr[r].first;
//cout<<"cc"<<endl;
}
else if(r<n && l<r){
int temp = sum;
for(int i = r;i>=l;i--){
int temp2 = temp;
//cout<<"till "<<i<<" ";
for(int x = l;x<=i;x++){
//cout<<"from "<<x<<" ";
temp2-=arr[x].first;
ori = max(ori , temp2);
}
//cout<<endl;
// cout<<temp2<<" ";
temp-=arr[i].first;
}
r++;
l = r;
if(l<n) sum = arr[l].first;
//cout<<l<<endl;
if(l<n) ori = max(arr[l].first , ori);
}
else if(l==r && r+1<n){
r++;
l = r;
sum = arr[l].first;
}
else break;
ori = max(ori , sum);
}
cout<<ori<<endl;
}
return 0;
}
*/