/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 0ms 420.0 KiB
#3 Accepted 0ms 284.0 KiB
#4 Accepted 0ms 320.0 KiB
#5 Accepted 1ms 324.0 KiB
#6 Accepted 0ms 324.0 KiB
#7 Accepted 1ms 324.0 KiB
#8 Accepted 0ms 404.0 KiB
#9 Accepted 10ms 816.0 KiB
#10 Accepted 10ms 808.0 KiB
#11 Accepted 10ms 812.0 KiB
#12 Accepted 1ms 324.0 KiB
#13 Accepted 1ms 320.0 KiB
#14 Accepted 9ms 804.0 KiB
#15 Accepted 9ms 788.0 KiB
#16 Accepted 8ms 812.0 KiB

Code

#include <stdio.h>

// Function to check if a number is even
int isEven(int num) {
    return num % 2 == 0;
}

// Function to find the maximum number of contiguous subarrays with even sum
int maxContiguousSubarrays(int arr[], int N) {
    int count = 0, even_sum = 0;
    
    for (int i = 0; i < N; i++) {
        // If the current element is odd, we need to increment the count of even sums
        if (!isEven(arr[i]))
            even_sum++;
        // If the number of odd elements encountered so far is even, we can form a subarray
        if (even_sum % 2 == 0)
            count++;
    }
    
    // If the total count of odd elements is even, we can form subarrays ending with the last element
    if (even_sum % 2 == 0)
        return count;
    else
        return -1;
}

int main() {
    int N;
    scanf("%d", &N);
    
    int arr[N];
    for (int i = 0; i < N; i++)
        scanf("%d", &arr[i]);
    
    printf("%d\n", maxContiguousSubarrays(arr, N));

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1042 Array partition
Contest
TLE_Headquarters - round #1
Language
C99 (GCC 13.2.0)
Submit At
2024-03-27 16:13:38
Judged At
2024-11-11 03:39:20
Judged By
Score
100
Total Time
10ms
Peak Memory
816.0 KiB