Problem: 2057B - Gorilla and the Exam
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 9):
#include <iostream>
#include <vector>

using namespace std;

int f(vector<int>& arr) { // No of distinct elements
    vector<int> distinct_ele;

    for (int num : arr) {
        bool ele_present = false;

        for (int ele : distinct_ele) {
            if (ele == num) {
                ele_present = true;
                break;
            }
        }

        if (!ele_present) {
            distinct_ele.push_back(num);
        }
    }

    return distinct_ele.size();
}

int main() {

    int t;
    cin >> t;

    for (int i = 0; i < t; i++) {
        int n, k;
        cin >> n >> k;

        if (!n) {
            cout << 0 << endl;
            continue;
        }

        vector<int> nums;
        vector<int> freq;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;

            bool x_found = false;

            for (int j = 0; j < nums.size(); j++) {
                if (nums[j] == x) {
                    freq[j]++;
                    x_found = true;
                    break;
                }
            }

            if (!x_found) {
                nums.push_back(x);
                freq.push_back(1);
            }
        }

        for (int i = 0; i < freq.size(); i++) {
            for (int j = 0; j < freq.size() - i - 1; j++) {
                if (freq[j] < freq[j+1]) {
                    freq[j] = freq[j+1] + freq[j] - (freq[j+1] = freq[j]);
                }
            }
        }

        int index = freq.size() - 1;
        while (k) {
            if (freq[index]) {
                freq[index]--;
                freq[0]++;
            } else {
                freq.pop_back();
                index--;
                freq[index]--;
                freq[0]++;
            }

            k--;
        }

        if (!freq[index]) {
            freq.pop_back();
            index--;
        }


        cout << freq.size() << endl;
    }

    return 0;
}


Submission (verdict: Wrong answer on test 1):
#include <iostream>
#include <vector>

using namespace std;

// Function to swap two elements
void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

// Function to partition the array and return the pivot index
int partition(vector<int>& arr, int low, int high) {
    // Choose a random pivot
    int pivotIndex = low + (high - low) / 2;
    int pivot = arr[pivotIndex];

    // Swap the pivot with the last element
    swap(arr[pivotIndex], arr[high]);

    int i = low - 1;
    for (int j = low; j < high; j++) {
        if (arr[j] > pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }

    // Swap the pivot with the element at the correct position
    swap(arr[i + 1], arr[high]);
    return i + 1;
}

// Function to implement Quicksort
void quicksort(vector<int>& arr, int low, int high) {
    if (low < high) {
        int pivotIndex = partition(arr, low, high);

        // Recursively sort the subarrays
        quicksort(arr, low, pivotIndex - 1);
        quicksort(arr, pivotIndex + 1, high);
    }
}

int main() {

    int t;
    cin >> t;

    for (int i = 0; i < t; i++) {
        int n, k;
        cin >> n >> k;

        if (!n) {
            cout << 0 << endl;
            continue;
        }

        vector<int> nums;
        vector<int> freq;
        for (int i = 0; i < n; i++) {
            int x;
            cin >> x;

            bool x_found = false;

            for (int j = 0; j < nums.size(); j++) {
                if (nums[j] == x) {
                    freq[j]++;
                    x_found = true;
                    break;
                }
            }

            if (!x_found) {
                nums.push_back(x);
                freq.push_back(1);
            }
        }

        quicksort(freq, 0, freq.size() - 1);

        int index = freq.size() - 1;
        while (k) {
            if (freq[index]) {
                freq[index]--;
                freq[0]++;
            } else {
                freq.pop_back();
                index--;
                freq[index]--;
                freq[0]++;
            }

            if (!index) {
                index = 0;
            }

            k--;
        }

        if (!freq[index]) {
            freq.pop_back();
            index--;
        }


        cout << ">" << freq.size() << endl;
    }

    return 0;
}