sorting || explained with example || c++
Intuition\n Describe your first thoughts on how to solve this problem. \nHow to think about this problem?\n    step 1: soting, because we have to maximize happi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHow to think about this problem?\n    step 1: soting, because we have to maximize happiness\n    step2 : only take care of all those values which are greater than 0.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n    1. Sort the array  in reverse order\n    2. take a sum varible to calculate happiness\n    3. run a while loop till k holds\n    4. rather than modifyng the array maintain a count varible to check what would be the happiness to remaining chilrdren.\n    5. because in every iteration remaing happiness decreases by 1\n    6. Example :\n        arr = [1,2,3,4,5] k = 4\n        after soting = 5,4,3,2,1\nAs k is greater than 0 always so take start sum = H[0] (that is 5);\n     k = k-1 = 3\n    for i = 1 and k = 3 \n        sum = 5 + 4-count = 5 + 3 ( as happiness would have decreased by 1 for\n all)\n    for i = 2 and k = 2 \n        sum = 9 - (3-2) ( we have again decreased remaing happiness by 1)\n    for i = 3 and = 3\n       H[3] = 2- count \n            = 2 -3 \n            = -1;\n        the loop will break because if happness of index arr[3] will become negaive.\n7. return the sum value \n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumHappinessSum(vector<int>& H, int k) {\n        sort(H.rbegin(),H.rend());\n        long long sum = H[0];\n        int i=1;\n        int count = 0;\n        k = k-1;\n        while(k-- && i<H.size()){\n            count++;\n            if(H[i]-count > 0){\n                sum += H[i] - count;\n            }\n            else{\n                break;\n            }\n            i++;\n        }\n        return sum;\n    }\n};\n```
['C++']