Happiest solution || Beginner
Intuition\nThe problem seems to involve finding the maximum sum of happiness among a given set of people, where each person\'s happiness is represented by an el
# Intuition\nThe problem seems to involve finding the maximum sum of happiness among a given set of people, where each person\'s happiness is represented by an element in the array. We need to select a subset of size k from the array such that the total happiness sum is maximized.\n\n# Approach\nOne possible approach is to sort the array in ascending order, indicating that the least happy people are at the beginning. Then, iterate through the sorted array from the end (most happy people) and adjust their happiness based on their position in the sorted array. Specifically, for each person, we subtract their current happiness by the number of people with higher happiness levels than them. This adjustment ensures that people with higher happiness contribute more to the total sum. We continue this process until we have selected k people or exhausted the 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        Arrays.sort(happiness);\n        int n=happiness.length;\n        int i=n-1;\n        long max=0;\n        while(k>0 && i>=0)\n        { \n            max+=Math.max(0,happiness[i]-(n-i-1));\n            i--;\n            k--; \n               \n        }\n        return max;  \n    }\n}\n```
['Array', 'Greedy', 'Java']