C , heapSort implementation beats 50%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```c []\n\n\nint parent(int i)\n{\n    if(i == 0)\n    {\n        return -1;\n    }\n\n    return ((i-1)/2);\n}\n\n\nint left(int i)\n{\n    return (2*i + 1);\n}\n\nint right(int i)\n{\n    return (2*i + 2);\n}\n\nvoid swap(int * a, int *b)\n{\n    int temp = *a;\n    *a = *b;\n    *b = temp;\n}\n\n// siftDown the first element\nvoid siftDown(int *arr, int heapSize)\n{\n    int i = 0;\n\n    int maxIndex = 0;\n    int l = 0;\n    int r = 0;\n    while(i < heapSize)\n    {\n        l = left(i);\n        r = right(i);\n        \n        maxIndex = i;\n\n        if((l < heapSize) && (arr[l] > arr[maxIndex]))\n        {\n            maxIndex = l;\n        }\n\n        if((r < heapSize) && (arr[r] > arr[maxIndex]))\n        {\n            maxIndex = r;\n        }\n\n        if(maxIndex != i)\n        {\n            swap(arr + i, arr + maxIndex);\n            i = maxIndex;\n        }\n        else\n        {\n            break;\n        }\n    }\n}\n\n// always siftUps the last element\nvoid siftUp(int *arr, int heapSize)\n{\n    int i = (heapSize - 1);\n\n    i = parent(i);\n    int l;\n    int r;\n    int maxIdx = 0;\n\n    while(i >= 0)\n    {\n        l = left(i);\n        r = right(i);\n\n        maxIdx = i;\n\n        if((l < heapSize) && (arr[l] > arr[maxIdx]))\n        {\n            maxIdx = l;\n        }\n\n        if((r < heapSize) && (arr[r] > arr[maxIdx]))\n        {\n            maxIdx = r;\n        }\n\n        if(maxIdx != i)\n        {\n            swap(arr + i, arr + maxIdx);\n        }\n\n        i = parent(i);\n    }\n}\n\nvoid heapInsert(int *arr, int heapSize, int new)\n{\n    // insert the new element at the after the last leaf\n\n    arr[heapSize] = new; // redundant\n\n    // always siftUps the last element\n    siftUp(arr, heapSize + 1);\n}\n\nvoid heapSort(int *arr, int len)\n{\n    int heapSize = 1;\n\n    // build the heap\n    for(int i = 1 ; i < len ; i++)\n    {\n        heapInsert(arr, heapSize, arr[i]);\n        heapSize++;\n    }\n\n    // extract max and move to the end\n    while(heapSize > 0)\n    {\n        // max is at arr[0]\n        swap(arr, arr + heapSize - 1);\n        \n        heapSize--;\n\n        // siftDown the first element\n        siftDown(arr, heapSize);\n    }\n\n\n    // array is sorted now\n    for(int i = 0 ; i < len ; i++)\n    {\n        printf("%d ", arr[i]);\n    }\n\n    printf("\\n");\n}\n\n\n\n\nint maximizeGreatness(int* nums, int numsSize)\n{\n    heapSort(nums, numsSize);\n\n    int permI = (numsSize - 1);\n\n    int cnt = 0;\n\n    for(int i = (numsSize - 2) ; i >= 0 ; i--)\n    {\n        if(nums[permI] > nums[i])\n        {\n            cnt++;\n            permI--;\n        }\n    }\n\n    return cnt;\n}\n```
['C']