/ SeriousOJ /

Record Detail

Accepted


  

Code

#include <bits/stdc++.h>
using namespace std;

int maxSubArraySum(int arr[], int size) {
    int max_so_far = INT_MIN; // Initialize max_so_far to a very small value
    int max_ending_here = 0; // Initialize max_ending_here to 0

    for (int i = 0; i < size; i++) {
        max_ending_here += arr[i]; // Add the current element to max_ending_here

        if (max_so_far < max_ending_here) {
            max_so_far = max_ending_here; // Update max_so_far if max_ending_here is greater
        }

        if (max_ending_here < 0) {
            max_ending_here = 0; // Reset max_ending_here to 0 if it becomes negative
        }
    }

    return max_so_far;
}

int main() {
    int N; // Number of positive integers
    cin >> N;

    int arr[N];
    for (int i = 0; i < N; i++) {
        cin >> arr[i];
    }

    int result = maxSubArraySum(arr, N);
    cout << "Maximum number of contiguous subarrays with even sum: " << result << endl;

    return 0;
}

Information

Submit By
Type
Pretest
Problem
P1042 Array partition
Language
C++20 (G++ 13.2.0)
Submit At
2024-03-27 16:45:11
Judged At
2024-11-11 03:38:09
Judged By
Score
0
Total Time
0ms
Peak Memory
0 Bytes