[Java/C++/Python] Easy and Concise, O(n)
Solution 1\nIntuition is to compare elements, need to sort first.\n\nTo get as many perm[i] > nums[i] as possible,\nwe will try to put smallest A[i] on the righ
# **Solution 1**\nIntuition is to compare elements, need to sort first.\n\nTo get as many `perm[i] > nums[i]` as possible,\nwe will try to put smallest `A[i]` on the right.\n\nSo we iterate `A`, and check if it\'s greater than `A[0]`.\nIf `A[0]` is done, we then try `A[1]` and so on.\n\nSo we can start with `res = 0` and `A[res]` is the next element to compare.\n\nFinally return `res` directly.\n\n\n# **Complexity**\nTime `O(sort)`\nSpace `O(sort)`\n<br>\n\n**Java**\n```java\n    public int maximizeGreatness(int[] A) {\n        Arrays.sort(A);\n        int res = 0;\n        for (int a : A)\n            if (a > A[res])\n                res++;\n        return res;\n    }\n```\n\n**C++**\n```cpp\n    int maximizeGreatness(vector<int>& A) {\n        sort(A.begin(), A.end());\n        int res = 0;\n        for (int& a : A)\n            if (a > A[res])\n                res++;\n        return res;\n    }\n```\n\n**Python**\n```py\n    def maximizeGreatness(self, A):\n        A.sort()\n        res = 0\n        for a in A:\n            if a > A[res]:\n                res += 1\n        return res\n```\n<br>\n\n# **Solution 2**\nImagine `A` is sorted,\nand we try to satify as many as possible but `k` elements.\n\nHow will we arrange the list?\n\nbefore: `A[0] A[1]   ... A[n - 1 - k]`\nafter:  `A[k] A[1+k] ... A[n - 1]`\n\nwhere\n`A[0] < A[k]`\n`A[1] < A[1 + k]`\n`A[2] < A[2 + k]`\n...\n`A[n - 1 - k] < A[n - 1]`\n\nThis greedy arrange is the best plan to satify as many pairs as possible.\nNote that `k` need to be bigger than the biggest frequency of `A[i]`,\notherwise there will be at least one `A[i] == A[i + k]`.\n\nSo the problem now is to find out the maximum frequency of `A`.\n<br>\n\n\n# **Complexity**\nTime `O(n)`\nSpace `O(n)`\n<br>\n\n**Java**\n```java\n    public int maximizeGreatness(int[] A) {\n        var count = new HashMap<Integer, Integer>();\n        int k = 0;\n        for (int a : A)\n            k = Math.max(k, count.merge(a, 1, Integer::sum));\n        return A.length - k;\n    }\n```\n\n**C++**\n```cpp\n    int maximizeGreatness(vector<int>& A) {\n        unordered_map<int, int> count;\n        int k = 0;\n        for (int a : A)\n            k = max(k, ++count[a]);\n        return A.size() - k;\n    }\n```\n\n**Python**\nIdea from @theabbie\n```py\n    def maximizeGreatness(self, A):\n        return len(A) - max(Counter(A).values())\n```\n
['C', 'Python', 'Java']