C++ | O(n) | 98.7% faster | Easy to understand | Clean code
Just maintain sum of all the elements between start and end of the subArray - 2 pointers Approach\n\nclass Solution {\npublic:\n\n    int getMax(vector &nums){\
**Just maintain sum of all the elements between start and end of the subArray - 2 pointers Approach**\n\nclass Solution {\npublic:\n\n    int getMax(vector<int> &nums){\n        int maxi = nums[0];\n        for(int i=1; i<nums.size(); i++){\n            if(nums[i] > maxi){\n                maxi = nums[i];\n            }\n        }\n        return maxi;\n    }\n\n    int maximumUniqueSubarray(vector<int>& nums) {\n        int start = 0;\n        int sum = 0;\n        int ans = INT_MIN;\n\n        int maxi = getMax(nums);\n        vector<int> store(maxi+1, -1);\n\n        for(int i=0; i<nums.size(); i++){\n            int temp = nums[i];\n\n            if(store[temp] != -1){\n                ans = max(ans, sum);\n\n                int index = store[temp];\n                while(start <= index){\n                    sum = sum - nums[start];\n                    store[nums[start]] = -1;\n                    start++;\n                }\n            }\n\n            sum += temp;\n            store[temp] = i;\n        }\n\n        ans = max(ans, sum);\n        return ans;\n        \n        bool like;\n        if(like){\n            cout << "Hit_upVote" << endl;\n        }\n    }\n};
[]