/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 332.0 KiB
#2 Accepted 1ms 284.0 KiB
#3 Accepted 1ms 340.0 KiB
#4 Accepted 1ms 332.0 KiB
#5 Accepted 1ms 332.0 KiB
#6 Accepted 1ms 516.0 KiB
#7 Accepted 1ms 284.0 KiB
#8 Accepted 1ms 328.0 KiB
#9 Accepted 20ms 816.0 KiB
#10 Accepted 20ms 812.0 KiB
#11 Accepted 20ms 1.004 MiB
#12 Accepted 1ms 328.0 KiB
#13 Accepted 1ms 284.0 KiB
#14 Accepted 16ms 808.0 KiB
#15 Accepted 16ms 808.0 KiB
#16 Accepted 16ms 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:12:55
Judged At
2024-11-11 03:39:23
Judged By
Score
100
Total Time
20ms
Peak Memory
1.004 MiB