✔💯DAY 363 || 100% || EXPLAINED || [JAVA/C++/PYTHON] || ONE PASS || O(1) || ONE LINER
\n\n\n# Intuition Approach\n Describe your approach to solving the problem. \n\n##### \u2022\tThe intuition behind this approach is that we want to maximize the
\n![image.png](https://assets.leetcode.com/users/images/5385f779-f8cc-4da1-a906-5e08f0066dc4_1680067386.260693.png)\n\n# Intuition Approach\n<!-- Describe your approach to solving the problem. -->\n\n##### \u2022\tThe intuition behind this approach is that we want to maximize the number of indices i for which perm[i] > nums[i] . To do this, we can sort the input array nums in non-decreasing order, and then create a new array perm such that perm[i] is the i -th largest element of nums . \n##### \u2022\tThis will ensure that the largest elements of nums are moved to the beginning of perm , and hence increase the number of indices i for which perm[i] > nums[i] . \n##### \u2022\tHowever, we don\'t actually need to create a new array perm to compute the maximum possible greatness. Instead, we can simply loop over the elements of nums and keep track of the greatest element seen so far. \n##### \u2022\tIf the current element is greater than the greatest element seen so far, we can increment the number of indices i for which perm[i] > nums[i] . \n##### \u2022\tHere\'s the step-by-step approach: \n##### \u2022\tSort the input array nums in non-decreasing order using the sort function from the library. \n##### \u2022\tInitialize a variable greatness to 0, which represents the maximum possible greatness. Loop over the elements of nums using a range-based for loop. \n##### \u2022\tFor each element it , check if it > nums[greatness] . If this condition is true, increment greatness by 1, since we can move the element it to index greatness in the permutation to increase the number of indices i for which perm[i] > nums[i] . \n##### \u2022\tReturn the value of greatness , which represents the maximum possible greatness. \n\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maximizeGreatness(vector<int>& nums) {\n    sort(nums.begin(), nums.end()); // Sort nums in non-decreasing order\n    int greatness = 0;\n    for (auto &it : nums) { // Loop over the elements of nums\n        if (it > nums[greatness]) { // If the current element is greater than the greatest element seen so far\n            greatness++; // Increment the number of indices i for which perm[i] > nums[i]\n        }\n    }\n    return greatness; // Return the maximum possible greatness\n}\n};\nstatic bool     _foo = ios::sync_with_stdio(false);\nstatic ostream* _bar = cin.tie(NULL);\n```\n```java []\npublic int maximizeGreatness(int[] nums) {\n    Arrays.sort(nums); // Sort nums in non-decreasing order\n    int greatness = 0;\n    for (int i = 0; i < nums.length; i++) { // Loop over the elements of nums\n        if (nums[i] > nums[greatness]) { // If the current element is greater than the greatest element seen so far\n            greatness++; // Increment the number of indices i for which perm[i] > nums[i]\n        }\n    }\n    return greatness; // Return the maximum possible greatness\n}\n```\n```python []\ndef maximizeGreatness(nums: List[int]) -> int:\n    nums.sort() # Sort nums in non-decreasing order\n    greatness = 0\n    for i in range(len(nums)): # Loop over the elements of nums\n        if nums[i] > nums[greatness]: # If the current element is greater than the greatest element seen so far\n            greatness += 1 # Increment the number of indices i for which perm[i] > nums[i]\n    return greatness # Return the maximum possible greatness\n```\n\n\n# Complexity\n\n##### \u2022\tThe time complexity of this approach is O(n log n), where n is the length of the input array nums , due to the sorting step. \n##### \u2022\tThe space complexity is O(1), since we only need to store a few integer variables.\n\n# 2ND WAY ONE LINER  TC = O(N) SC = O(1)\n\nMY POST LINK https://leetcode.com/problems/maximize-greatness-of-an-array/solutions/3354150/day-363-100-explained-java-c-python-one-pass-o-1-one-liner/\n\n```PYTHON []\n def maximizeGreatness(nums: List[int]) -> int:\n        return len(nums) - max(Counter(nums).values())\n```\n```JAVA []\n public int maximizeGreatness(int[] nums) {\n    return nums.length - Collections.max(Arrays.stream(nums).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting())).values()).intValue();\n}\n```\n```C++ []\n   int maximizeGreatness(vector<int>& nums) {\n    std::unordered_map<int, int> freq;\n    for (int x : nums) freq[x]++;\n    return nums.size() - std::max_element(freq.begin(), freq.end(), [](const auto& a, const auto& b) { return a.second < b.second; })->second;\n}\n```\n# DRY RUN \n```\nnums = [1,3,5,2,1,3,1]\nnums.sort() # nums = [1,1,1,2,3,3,5]\ncounter = Counter(nums) # counter = {1: 3, 3: 2, 5: 1, 2: 1}\nmaxCount = max(counter.values()) # maxCount = 3\nreturn len(nums) - maxCount # return 7 - 3 = 4\n```\n\n##### \u2022\tThe first line, static bool _foo = ios::sync_with_stdio(false); , disables the synchronization between the C++ standard streams (e.g., cin , cout , cerr , etc.) and the C standard streams (e.g., stdin , stdout , stderr , etc.). \n##### \u2022\tBy default, these streams are synchronized, which means that input/output operations on one stream may cause the other stream to flush its buffer. This can be slow, especially when dealing with large amounts of input/output data. \n##### \u2022\tDisabling the synchronization can improve the performance of input/output operations, but it may also cause some unexpected behavior if the streams are used in a mixed manner (i.e., both C++ and C-style input/output operations are used). \n##### \u2022\tThe second line, static ostream* _bar = cin.tie(NULL); , unties the cin stream from the cout stream. By default, the cin stream is tied to the cout stream, which means that the cout stream is flushed before any input is read from the cin stream. \n##### \u2022\tThis can also be slow, especially when dealing with large amounts of input/output data. Untying the streams can improve the performance of input/output operations, but it may also cause some unexpected behavior if the streams are used in a mixed manner. \n##### \u2022\tNote that these lines of code are not necessary in most cases, and may even cause some unexpected behavior if used incorrectly. They are typically used in performance-critical applications where input/output operations are a bottleneck, and where the behavior of the standard streams can be carefully controlled.\n\n\n![BREUSELEE.webp](https://assets.leetcode.com/users/images/a387cd36-9f55-470e-aa7c-a89af19fe7bb_1680030050.9267182.webp)\n![meme2.png](https://assets.leetcode.com/users/images/145cb6b9-c58f-4218-93bb-9845b4793151_1680030060.7843618.png)\n\n
['Python', 'C++', 'Java', 'Python3']