# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
985207 | 2024-05-17T12:42:35 Z | CrazyBotBoy | Sequence (APIO23_sequence) | C++17 | 0 ms | 0 KB |
#include "sequence.h" #include <vector> #include <unordered_map> int sequence(int N, std::vector<int> A) { // Create a map to store count of each element in the sequence std::unordered_map<int, int> count; for (int num : A) { count[num]++; } int max_median_count = 0; // Iterate through all subarrays for (int l = 0; l < N; ++l) { int current_median_count = 0; for (int r = l; r < N; ++r) { // Update count for the current element in the subarray count[A[r]]--; // Check if the current element is a potential median int potential_median = A[r]; if (count[potential_median] >= (r - l + 1) / 2 && count[potential_median] <= (r - l + 2) / 2) { // Update current_median_count if the element can be a median current_median_count = std::max(current_median_count, count[potential_median]); } max_median_count = std::max(max_median_count, current_median_count); } // Reset count for the elements processed in the current subarray for (int i = l; i <= r; ++i) { count[A[i]]++; } } return max_median_count; }