[Java] Sliding Window + HashMap | With Explanation
the question is similar with 3. Longest Substring Without Repeating Characters\n\n\n### Intuition\nthe goal is to find the max sum subarray with unique elements
> the question is similar with [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)\n\n\n### Intuition\nthe goal is to find the max sum subarray with unique elements\nsince the elements are positive numbers, we can use sliding window approach\n\n### Steps\nlet `j`,` i` is the left, right boundary of sliding window\n1. increment `i` to include more elements, and update the sum & element count\n2. if the `element count > 1`, increment `j` and update the sum & element count, until `element count <= 1`\n3. update the result to `max(result, sum)`\n4. repeat step 1-3\n\n### Complexity\ntime: `O(n)`\nspace: `O(n)`\n### Java\n```java\npublic int maximumUniqueSubarray(int[] A) {\n    int n = A.length;\n    Map<Integer, Integer> map = new HashMap<>(); // element, count\n\n    int j = 0, sum = 0, res = 0;\n    for (int i = 0; i < n; i++) {\n        map.put(A[i], map.getOrDefault(A[i], 0) + 1);\n        sum += A[i];\n\n        while (map.get(A[i]) > 1) {\n            map.put(A[j], map.get(A[j]) - 1);\n            sum -= A[j];\n            j++;\n        }\n        res = Math.max(res, sum);\n    }\n    return res;\n}\n```\n
['Java']