1def max_length(s, k):
2 current = []
3 max_len = -1 # returns -1 if there is no subsequence that adds up to k.
4 for i in s:
5 current.append(i)
6 while sum(current) > k: # Shrink the array from the left, until the sum is <= k.
7 current = current[1:]
8 if sum(current) == k:
9 max_len = max(max_len, len(current))
10
11 return max_len
1#include <iostream>
2
3using namespace std;
4
5int main(){
6 //Input Array
7 int n;
8 cin >> n;
9 int arr[n];
10 for(int i =0;i< n;i++){
11 cin >> arr[i];
12 }
13
14 int currentSum = 0;
15 int maxSum = INT_MIN;
16 //algo
17 for (int i = 0; i < n; i++)
18 {
19 currentSum += arr[i];
20 if (currentSum <0)
21 {
22 currentSum = 0;
23 }
24 maxSum = max(maxSum, currentSum);
25 }
26 cout << maxSum << endl;
27
28 return 0;
29}