✅ Easiest Java Solution 🔥Step By Step || Deatailed Expalined ✅
Intuition\nSince question is to count the maximum number of score you can achieve . \nAnd the score of nums is the number of positive integers in the array pref
# Intuition\nSince question is to count the maximum number of score you can achieve . \nAnd the score of nums is the number of positive integers in the array prefix and prefix is the array containing the prefix sums of nums after rearranging it.\n In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it.\n\n In order to maximize the score we need to keep all the positive element continuously. **We can do it by sorting the given array.**\n Here no need to take extra space for prefix sum, since we just have to count the number of positive sum we can store it in variable and carry through the whole array.\n\n**One more catch** is that we need to take sum variable long because of the constraint given ***(-106 <= nums[i] <= 106)*** . If we will store sum in integer varaible it might give you wrong sum due to integer overflow.\n Then iterate throught the array, add arr[i] to sum, If at the current index sum>0 add 1 to answer.\n\n# Approach\n> Step 1: Sort the given array.\n\n**You can sort it in descending order as well but I have sorted int ascending order.**\n\n> Step 2: Intialize a variable sum(long) = 0;\n\n> Step 3: Intialize a variable max(int) = 0, in which we will store answer.\n\n> step 4: Traverse the array from right to left (if array is sorted in ascending order) and left to right (if array is sorted in descending order), add current index element to sum and check if it is greater than 0, if it is add 1 to answer.\n\n> step 5: Return answer.\n# Complexity\n- Time complexity: $$O(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\n# Code\n```\nclass Solution {\n    public int maxScore(int[] nums) {\n\n        Arrays.sort(nums);\n        long sum=0;\n        int ans=0;\n        int n=nums.length-1;\n        \n        for(int i=n;i>=0;i--){\n            sum+=nums[i];\n            if(sum>0)ans++;\n        }\n\n        return ans;\n    }\n}\n```\n``` []\nThanks for viewing the solution.\nHope it helps, if it does, please leave a comment below \uD83D\uDE4F\nAnd do consider UPVOTING \u2B06\uFE0F Because it motivates me,\nwriting such solution.\n```\n \n![please_upvote.jpeg](https://assets.leetcode.com/users/images/7349644a-8b47-4bc8-a9e0-5d39aa224036_1678591128.170461.jpeg)
['Array', 'Sorting', 'Prefix Sum', 'Java']