✔Easy 🔥Intuition 🟢Full explanations 🚀 All Lan code
Logic\n This problem is about to find the maximum sum of subarrray (where elements in subarray is unique) in an array.\n This is a classic Sliding Window proble
**Logic**\n* This problem is about to find the maximum sum of subarrray (where elements in subarray is unique) in an array.\n* This is a classic Sliding Window problem which is simillar to this problem 3. Longest Substring Without Repeating Characters\n* We use seen (HashMap) to keep track of last index of a number.\n* We use sum to keep sum of sub array in range [l..r] so far where elements are unique.\n* While extend right side r if we met an existed number then we move left side l until seen[nums[r]] + 1 and of course, we need to decrease sum corresponding.\n\n**Complexity**\n\n* Time: O(N), where N is number of elements in array nums.\n* Space: O(M), where M <= N is the maximum number of distinct numbers in an subarray.\n\n**Python**\n\n```\nclass Solution:\n    def maximumUniqueSubarray(self, nums: List[int]) -> int:\n        seen = dict()\n        ans = sum = 0\n        l = 0\n        for r, x in enumerate(nums):\n            if x in seen:\n                index = seen[x]\n                while l <= index:  # Move the left side until index + 1\n                    del seen[nums[l]]\n                    sum -= nums[l]\n                    l += 1\n\n            seen[x] = r\n            sum += x\n            ans = max(ans, sum)\n        return ans\n```\n\n**C++**\n\n\n```\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& nums) {\n        unordered_map<int, int> seen;\n        int l = 0, sum = 0, ans = 0;\n        for (int r = 0; r < nums.size(); r++) {\n            int x = nums[r];\n            if (seen.find(x) != seen.end()) {\n                int index = seen[x];\n                while (l <= index) {  // Move the left side until index + 1\n                    seen.erase(nums[l]);\n                    sum -= nums[l];\n                    l += 1;\n                }\n            }\n            seen[x] = r;\n            sum += x;\n            ans = max(ans, sum);\n        }\n        return ans;\n    }\n};\n```\n\n**Java**\n\n```\nclass Solution {\n    public int maximumUniqueSubarray(int[] nums) {\n        Map<Integer, Integer> seen = new HashMap<>();\n        int l = 0, sum = 0, ans = 0;\n        for (int r = 0; r < nums.length; r++) {\n            int x = nums[r];\n            if (seen.containsKey(x)) {\n                int index = seen.get(x);\n                while (l <= index) {  // Move the left side until index + 1\n                    seen.remove(nums[l]);\n                    sum -= nums[l];\n                    l += 1;\n                }\n            }\n            seen.put(x, r);\n            sum += x;\n            ans = Math.max(ans, sum);\n        }\n        return ans;\n    }\n}\nC++\n\nclass Solution {\npublic:\n    int maximumUniqueSubarray(vector<int>& nums) {\n        unordered_map<int, int> seen;\n        int l = 0, sum = 0, ans = 0;\n        for (int r = 0; r < nums.size(); r++) {\n            int x = nums[r];\n            if (seen.find(x) != seen.end()) {\n                int index = seen[x];\n                while (l <= index) {  // Move the left side until index + 1\n                    seen.erase(nums[l]);\n                    sum -= nums[l];\n                    l += 1;\n                }\n            }\n            seen[x] = r;\n            sum += x;\n            ans = max(ans, sum);\n        }\n        return ans;\n    }\n};\n```
['C', 'Python', 'Java']