✅✅Fastest 💯💯|| Simplest 🧠🧠 || Greedy
Thanks for dropping by\uD83D\uDC4B, I hope it helps :) \n\n# Intuition \uD83D\uDC31\u200D\uD83C\uDFCD\n Describe your first thoughts on how to solve this proble
## Thanks for dropping by\uD83D\uDC4B, I hope it helps :) \n\n# Intuition \uD83D\uDC31\u200D\uD83C\uDFCD\n<!-- Describe your first thoughts on how to solve this problem. -->\n### Sochne\uD83E\uDDE0 ka hi khel hai saara, aur jab iss question\uD83E\uDD14 ko dekha aur socha toh samaj aaya ki isme aisa kuch aisa istemaal ho raha jo meri zindagi mai nahi hora --- sahi samjhe aap _Sorting_\n### Ab sawal\uD83D\uDCDD ko medium bola hai toh thoda mushkil lagna aam baat hai, magar ek baar tum iss solution\uD83C\uDFAF ko padh loge toh tum paaoge ki mushkil tha nahi par jo shuru mai dar aa gaya mann\uD83E\uDDE0 mai usne karne nahi diya bs\n\n# Approach \uD83D\uDCA1\n<!-- Describe your approach to solving the problem. -->\n### Ab zindagi sorted nahi hai par iss question ko niptane ke liye hum sorting use karenge. \n### Ek baar question ko tameez se padh\uD83D\uDCDA loge toh ek jordaar\uD83D\uDD25 baat samaj aayegi ki, apne ko toh keval K bacho ki khushiyaan\uD83D\uDE01 jodni hai (Relate karna band karo, ki placement mai bhi K bacho ko hi Khushi mili aur tumhara number `K+1` tha, Khair choro, magar Anmol bhi buri ladki nahi hai....)\n### Ab dhyaan\uD83D\uDE07 bhatakna nahi chahiye, kyuki K bache pehle hi aage \uD83C\uDFC3\u200D\u2642\uFE0F chal rahe hain, aur inko hi pakad ke hum iss sawal ko iski manjil\uD83D\uDEE3 tak le jaana hai\n### Humko karna hai Happiness\uD83D\uDE01 Maximise, toh maximum ke liye humko chahiye sabse badi values. Yaha pe tum samaj gye ki sort karna padega khushiyon ko....\n`(Leetcode Giving life lessons through question, samjho...)`\n##### Ab chadta hua\uD83D\uDCC8 (ascending) ya girta hua\uD83D\uDCC9 (descending) sort tumhari marji hai, magar karna toh hai( Priority queue based solution bhi chahiye ho toh bta dena comments mai, iss question ke liye mujhe laga nahi ki maamla complex\uD83E\uDDE0 karne ki need hai, so nahi daal raha hu, kyuki mudda hai solve krna kamse kamse lafdo mai)\n\n#### Ab koi khush hoga toh usko barabar karne ke liye prakriti kisi ko toh dukh degi hi, yahi bola hai question mai ki ek bache ki Khushiya jodne ke baad baaki bache huye bacho ki khushiyan kam hongi (har dukh pe relate mat kiya karo, agle kisi question mai tum hi select hoge, bas kaam krte jao, aur mere solution ko upvote karke, meri Dua lo) Dua Lipa mat socho bhyii, maana sunder hai\uD83D\uDC95\n\n### Ab kiski kitni khushiyan kam hongi\uD83D\uDE22 iske liye hum ek `happinessReduced` variable ko bharte rahenge. Jab bhi kisi khusiyan jod di jayengi tab tab iss dukh ke garhe \uD83D\uDDD1(garhe ka icon ni mila) ko bhardenge hum, Dukh hai par seh lenge....\n\n### Aur jab bhi kisi ki khushiyaan jodni hogi toh dekh lenge ki kya vo dukh ke garhe se jyada hai ya nahi, \n- Agar haan\u2705, toh dono ke difference ko total khushiyon mai jod denge, aur dukh ka garha bharkar aage badhenge\n- Agar nahi\u274C toh fir hum dhundna hi band kar denge, kyuki iska matlab ab dukh khushiyo se bada ho gaya hai, aur loop mai bane rehna bilkul vesa hi hoga jese tum the uski wapsi ki yaad mai ho jaate ho....(__Ekdum Faltu__)\n\n# Algorithm \uD83D\uDC69\u200D\uD83D\uDCBB\n\n### Ek sawal ke nazariye baatein ho gyi kaafi ab maine sprite pee li hai, isiliye no bakwaas\n\n#### Samasya Ka Vivaran - Sawal Ek nazar\uD83D\uDC53 mai\n\n#### Di gayi hai ek integer array `happiness` aur ek integer `k` , aapko apne total khushi ki sum ko adhik se adhik banana hai.\n\n#### Aapko jyada se jyada `k` elements ko array se lena hai aur kam se kam khushi ghatane ki anumati hai. Ek element ki khushi ko 0 tak ghataaya ja sakta hai, lekin 0 se kam nahi, kyuki jo khushi kisi pe hai hi nahi, usko kese hi koi le sakta hai.\n\n#### `maximumHappinessSum` function ko implement karna hai, jo jyada se jyada `k` elements ko istemaal karne ke baad adhik se adhik khushi ke sum ko vaapas kare.\n\n\n1. **Happiness values ko ascending order mein sort karo.**\n2. **Sort ki gayi happiness values ko reverse order mein traverse karo.**\n    - **Agar vartaman Khushiya dukh se kam ho toh, total khushiya ko vaapas karo.**\n    - **Anytha Vartaman khushi mai dukh ghata kar, total Khushiyo mein jodo.**\n    - **Dukh ko badhao.**\n3. **Total adhikatam khushi ke sum ko vaapas karo.**\n\n\n# Complexity \uD83C\uDFCB\uFE0F\u200D\u2640\uFE0F\n- Time complexity: $$O(N * Log(N))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![image.png](https://assets.leetcode.com/users/images/9f214bf8-2f99-4c4b-b828-160d84ad7c05_1715239292.4407222.png)\n\n\n# Code\uD83D\uDCBB\n```java []\nclass Solution\n{\npublic\n    long maximumHappinessSum(int[] happiness, int k)\n    {\n        Arrays.sort(happiness); // Sort the happiness values in ascending order\n        long totalHappiness = 0, happinessReduced = 0;\n        int len = happiness.length;\n\n        // Traverse the sorted happiness values in reverse order\n        for (int i = len - 1; k > 0; i--, k--)\n        {\n            // If reducing the happiness value by the current track value makes it non-positive, return the total happiness\n            if (happiness[i] - happinessReduced <= 0)\n                return totalHappiness;\n            totalHappiness += happiness[i] - happinessReduced; // Add the current maximum happiness value to the total\n            happinessReduced++;                                // Increment the track value\n        }\n\n        return totalHappiness; // Return the total maximum happiness sum\n    }\n}\n```\n```cpp []\nclass Solution\n{\npublic:\n    long long maximumHappinessSum(vector<int> &happiness, int k)\n    {\n        sort(happiness.begin(), happiness.end()); // Sort the happiness values in ascending order\n        long long totalHappiness = 0, happinessReduced = 0, len = happiness.size();\n\n        // Traverse the sorted happiness values in reverse order\n        for (int i = len - 1; k > 0; i--, k--)\n        {\n            // If reducing the happiness value by the current track value makes it non-positive, return the total happiness\n            if (happiness[i] - happinessReduced <= 0)\n                return totalHappiness;\n            totalHappiness += happiness[i] - happinessReduced; // Add the current maximum happiness value to the total\n            happinessReduced++;                                // Increment the track value\n        }\n\n        return totalHappiness; // Return the total maximum happiness sum\n    }\n};\n```\n```python3 []\nfrom typing import List\n\nclass Solution:\n    def maximumHappinessSum(self, happiness: List[int], k: int) -> int:\n        # Sort the happiness values in ascending order\n        happiness.sort()\n\n        total_happiness = 0  # Variable to store the total happiness sum\n        happiness_reduced = 0  # Variable to store the amount of happiness reduced\n        length = len(happiness)  # Get the length of the happiness list\n        i = length - 1  # Start from the last element of the sorted happiness list\n\n        # Traverse the sorted happiness values in reverse order for \'k\' iterations\n        for _ in range(k):\n            # If reducing the happiness value by the current track value makes it non-positive,\n            # it means we\'ve reached the minimum possible happiness, so we return the total happiness\n            if happiness[i] - happiness_reduced <= 0:\n                return total_happiness\n            \n            # Add the current maximum happiness value (after reducing) to the total happiness sum\n            total_happiness += happiness[i] - happiness_reduced\n            \n            # Increment the track value as we reduce happiness for the next iteration\n            happiness_reduced += 1\n            \n            # Move to the next element in the sorted happiness list\n            i -= 1\n\n        # Return the total maximum happiness sum\n        return total_happiness\n\n```\n\n![upv4.jpeg](https://assets.leetcode.com/users/images/fb2e9ff5-eb2e-4d3e-bf86-839b54d28c95_1715244284.213541.jpeg)\n\n
['Array', 'Greedy', 'Sorting', 'C++', 'Java', 'Python3']