cpp solution || hash table || 99.76 % faster and 99.27% space efficient || Two-Pointer
Thank you for checking out my solution\nDo upvote if it helped :)\n_\n_Approach 1 : Brute Force (Will Fail as per the Constraints)\n__Brute force approach could
__Thank you for checking out my solution\nDo upvote if it helped :)__\n____\n__Approach 1 : Brute Force (Will Fail as per the Constraints)__\n__Brute force approach could be to set a quadratic algorithm\nSet up an answer variable as ans = 0 \nInitialise a new set__\n__Where the outer loop marks a starting position__\n>Sum = value at the current index\n>The inner loop will decide the ending index, based on the first time an element is repeated\n>>If the element is repeated\n>>>__Store the max of the current sum and the ans \n>>>Empty the set\n>>>Add the current element to the set\n>>>Break the loop__\n>>>\n>>Else,\n>>>__Add the elements to the current sum\n>>>insert the element to the set__\n>>>\n__Return the Answer__\n\n__Given below is the Coding implementation for the same__\n\n```\nclass Solution \n{ \npublic:\n    int maximumUniqueSubarray(vector<int>& nums) \n    {\n        ios_base::sync_with_stdio(0);\n        unordered_set<int> st;\n        int sum = 0, ans = 0;\n        for(int i = 0; i < nums.size(); i++)\n        {\n            st.insert(nums[i]);\n            sum = nums[i];\n            for(int j = i+1; j < nums.size(); j++)\n            {\n                if(st.find(nums[j]) != st.end())\n                {\n                    st.clear();\n                    ans = max(ans, sum);\n                    break;\n                }\n                sum += nums[j];\n                st.insert(nums[j]);\n                ans = max(ans,sum);\n            }\n        }\n        return max(ans,sum);\n    }\n};\n```\n__Time Complexity : O(N^2)\nSpace Complexity: O(N)__\n____\n__Approach 2 : Two-Pointers/Sliding Window__\n__Based on the constraints, if you were to try the brute force, you will surely get TLE.\nNow the task at hand is to write an algorithm that works in O(N) time [O(N. log(N)) will also work, if you find one, do comment below]__\n\n__If you closely observe the brute force solution, you will notice that when a running sum is calculated, once an element is repeated, \nall the elements occuring upto its previous occurance can be discarded altogether.\nFor example\nnums = [1,2,3,4,5,6,7,7,8,9]\nNow if start to calculate the sum from any index between 0 to 7, it will always end at 8, because the 7th element repeats\nThat means any element that repeats nullifies the contribution of all the elements that occurred before its previous index.\nLike in our example, the brute force algorithm will sum for all the indices in [0,6], only to break at index 7 everytime for first 7 interations.__\n____\n__Now that we have seen that brute force algorithm is repetitive, we can optimise it to get a better time complexity\nWe already know by now, that we need to keeep a track of indices of the elements, so we will use a frequency array, to do so__\n\n__1. Create a array [mapp] with length = max(in the array) + 1, for simplicity I used the upperbound of the constraints\n2. Create a pointer prev, to store the startingg index of the current sum\n3. Create a sum variable to store the current sum\n4. Create an answer variable to store the maximum of all the running sums__ \n\n__Lets begin with the algorithm__\n>__Iterate from 0 to size of the vector:__\n>>__Store the current element in a variable , say x, this prevents the back to back accessing of the element\n>>Check if the current position has been traversed (If Traversed, then surely there would be an index value at this position)__\n>>>__If traversed\n>>>Store the index stored at the elements position in the map in variable pos( you can name it anything you want);\n>>>Now Iterate from prev to this stored index pos:__\n>>>>Keep Subtracting the elements occuring in the nums from index [prev, pos)\n>>>>\n>>__Store the current index + 1 in mapp[x], mapp[x] = current index + 1\n>>Add the element in x to the sum, sum += x\n>>Store the max of running sum in the answer variable, ans = max(ans, sum)__\n>>\n__Return the answer variable__\n____\n\n__Below Is the coding implementaion for the same__\n```\nclass Solution\n{ // Runtime: 143 ms, faster than 99.76% of C++ online submissions for Maximum Erasure Value.\n  // Memory Usage: 89.2 MB, less than 99.27% of C++ online submissions for Maximum Erasure Value.\npublic:\n    int maximumUniqueSubarray(vector<int> &nums)\n    {\n        int a[10001] = {0};             // Creating a hashtable using the array, initialisng all the values to 0\n                                        // Stating that No element has been traversed yet\n        int prev = 0, sum = 0, ans = 0; // Using prev to store the index of the last deleted element\n                                        // Sum to calculate the running sum, ans to store the sum of maximum unique subarray\n\n        for (int i = 0; i < nums.size(); i++)\n        {\n            int x = nums[i]; // Storing the current element in the vector nums, prevents the back to back access of elements\n\n            if (a[x] != 0)               // If the hash value is not 0 for the element, that means it has occurred before\n            {                            // Time to Decrease the available window\n                int t = a[x];            // Store the previous index of this element\n                while (prev < t)         // Subtract (from the sum) all the elements occurring in the vector from [prev, t), notice the brackets, \n                                         // indicating that element at prev is inclusive, but not the one at t is to be excluded\n                    sum -= nums[prev++]; // The value of sum decreases, \n            }\n            //Now Our Sliding window is of the size [i - prev + 1]\n            a[x] = i + 1;        // Storing the occuring index of the current element\n            sum += x;            // Aadding the value to the sum\n            ans = max(ans, sum); // Storing the max sum in the answer variable\n        }\n        return ans;\n    }\n};\n```\n__Time Complexity : O(N)\nSpace Complexity: O(1e4 + 1)__
['Two Pointers', 'Sliding Window', 'C++']