✅Easiest Solution🔥||Beat 97.44%🔥||Beginner Friendly✅🔥
Intuition\nSorting: The code begins by sorting the array happiness in ascending order using Arrays.sort(happiness);. This step ensures that the array is in asce
# Intuition\nSorting: The code begins by sorting the array happiness in ascending order using Arrays.sort(happiness);. This step ensures that the array is in ascending order, making it easier to calculate the maximum happiness sum later.\n\nCalculating Maximum Happiness Sum:\nThe algorithm initializes max to 0 to keep track of the maximum happiness sum.\nIt sets n as the length of the happiness array and i as the index pointing to the last element of the sorted array (n - 1).\nThe while loop continues until k becomes 0 or i goes below 0.\nInside the loop, it checks if i is greater than or equal to 0.\nIf true, it calculates the maximum between 0 and happiness[i] - (n - i - 1) and adds this value to max.\nIt then decrements i and k by 1 in each iteration.\n\nReturning Maximum Happiness Sum: \nAfter the loop finishes, the algorithm returns the final value of max, which represents the maximum happiness sum after subtracting k largest elements from the sorted array.\n\n# Complexity\n- Time complexity:\n$$O(nlogn)$$\n\n- Space complexity:\n $$O(1)$$\n\n# Code\n```\nclass Solution {\n    public long maximumHappinessSum(int[] happiness, int k) {\n\n        Arrays.sort(happiness);\n        long max = 0;\n        int n = happiness.length;\n        int i = n-1;\n\n        while(k>0 && i>=0)\n        {\n            if(i>=0)\n            {\n                \n                max += Math.max(0, happiness[i] - (n - i-1 ));\n            i--;\n            k--;\n\n            }\n        }\n        return max;\n    }\n}\n\n```
['Array', 'Greedy', 'Sorting', 'Java']