Untitled
unknown
c_cpp
3 months ago
1.0 kB
4
Indexable
#include <iostream> #include <vector> using namespace std; int maxEvenSumPairs(vector<int> &A) { int n = A.size(); vector<bool> paired(n, false); // Track whether an element is already paired int count = 0; for (int i = 0; i < n; i++) { int next = (i + 1) % n; // Circular indexing if (!paired[i] && !paired[next] && (A[i] + A[next]) % 2 == 0) { paired[i] = paired[next] = true; count++; } } return count; } int main() { vector<vector<int>> testCases = { {4, 2, 5, 8, 7, 3, 7}, // Expected output: 2 {1, 3, 5, 7}, // Expected output: 2 {2, 4, 6, 8}, // Expected output: 2 {1, 2, 3, 4, 5, 6}, // Expected output: 0 {10, 20, 30, 40, 50} // Expected output: 2 }; for (int i = 0; i < testCases.size(); i++) { cout << "Test Case " << i + 1 << ": "; cout << maxEvenSumPairs(testCases[i]) << endl; } return 0; }
Editor is loading...
Leave a Comment