id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> inDegree(n, 0);\n for (int i =0; i <n; ++i) \n {\n if (leftChild[i] != -1) \n {\n inDegree[leftChild[i]]++;\n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> inDegree(n, 0);\n\n // Calculate in-degrees to identify the root and check for multiple parents\n for (int i = 0; i < n; ++i) {\n if (leftChild[i] != -1) {\n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n int findroot(unordered_set<int>st,int n){\n for(int i = 0; i < n; i++) {\n if(st.find(i) == st.end()) {\n return i;\n }\n }\n return -1;\n }\n bool validateBinaryTreeNodes(int n, vector<int>& lft, vector<int>& rg...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n unordered_map<int, vector<int>> mp;\n vector<int> inDegree(n, 0);\n int i = 0;\n for (i = 0; i < n; ++i)\n {\n if (leftChild[i] != -1) \n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n unordered_set<int> visited;\n bool valid = true;\n void dfs(vector<int>& leftChild, vector<int>& rightChild, int node){\n if(!valid||visited.find(node)!=visited.end()){\n valid = false;\n return;\n }\n visited.insert(node);\n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool dfs(int i,vector<vector<int>>&g,int visited[]){\n visited[i]=1;\n for(auto child:g[i]){\n if(!visited[child]){\n if(dfs(child,g,visited)){\n return true;\n }\n }\n else{\n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\n\npublic:\n bool isCyclic(int src, int par, vector<vector<int> >& adj, vector<bool>& vis) {\n vis[src] = true;\n for(int child : adj[src]) {\n if(!vis[child]) {\n if(isCyclic(child, src, adj, vis) ) return true;\n }\n else i...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n void helper(auto&adj,auto&vis,int par){\n vis[par] = true;\n for(auto x:adj[par]){\n if(!vis[x]){\n helper(adj,vis,x);\n }\n }\n }\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightC...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n unordered_map<int,vector<int>>mp;\n vector<bool>vis;\n int dfs(int x){\n vis[x]=1;\n // cout<<x<<endl;\n int cnt=0;\n for(auto it:mp[x]){\n if(vis[it])return 0;\n int val = dfs(it);\n if(!val)return 0;\n ...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool cycle(unordered_map<int,vector<int>> &adj,int cur,vector<int> &vis) {\n vis[cur]=1;\n\n for (auto nbr:adj[cur]) {\n if(vis[nbr]==1) return true; \n if(vis[nbr]==0) {\n if(cycle(adj,nbr,vis)) return true;\n }\n...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool dfs(int node,vector<int> adj[],vector<bool>& vis,vector<bool>& recStack){\n vis[node]=true;\n recStack[node]=true;\n for(auto it: adj[node]){\n if(!vis[it]){\n if(dfs(it,adj,vis,recStack)){\n return true;\...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool dfs(int node,vector<int> adj[],vector<bool>& vis,vector<bool>& recStack){\n vis[node]=true;\n recStack[node]=true;\n for(auto it: adj[node]){\n if(!vis[it]){\n if(dfs(it,adj,vis,recStack)){\n return true;\...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {\n vector<int> indegree(n);\n unordered_set<int> st;\n for(int i=0;i<n;i++){\n if(leftChild[i]!=-1){\n if(leftChild[i]>=n ||indegree[leftChild[...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool dfs(int node, map<int, vector<int>>& adj, vector<int>& vst) {\n vst[node] = 1; // grey\n for (auto v : adj[node]) {\n if (vst[v] == 1)\n return 1;\n else if (vst[v] == 0 and dfs(v, adj, vst)) {\n // if whi...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\nint find(int x, vector<int>&parent){\n if(parent[x]==x)return x;\n\n return parent[x]=find(parent[x],parent);\n}\n\nvoid unionn(vector<int>&parent,vector<int>&rank,int x,int y){\n int x_parent=find(x,parent);\n int y_parent=find(y,parent);\n\n if(x_parent!=y_p...
1,275
<p>You have <code>n</code> binary tree nodes numbered from <code>0</code> to <code>n - 1</code> where node <code>i</code> has two children <code>leftChild[i]</code> and <code>rightChild[i]</code>, return <code>true</code> if and only if <strong>all</strong> the given nodes form <strong>exactly one</strong> valid binary...
3
{ "code": "class Solution {\npublic:\n bool dfs(vector<int>adj[], map<int,int>&vis,int node)\n {\n vis[node]=1;\n for(auto e:adj[node])\n {\n if(vis.find(e)!=vis.end())\n {\n return false;\n }\n if(!dfs(adj,vis,e))\n retu...
172
<p>Given an integer <code>n</code>, return <em>the number of trailing zeroes in </em><code>n!</code>.</p> <p>Note that <code>n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 3 <strong>Output:</strong> 0 <strong...
0
{ "code": "class Solution {\npublic:\n\n // Trailing zeros\n\n // count the pairs 2 * 5 becauss that will make 10 that have 1 zero \n\n\n \n int trailingZeroes(int n) {\n int count = 0;\n\n while(n>=5){\n n /= 5;\n count += n;\n }\n return count;\n }\n\...
172
<p>Given an integer <code>n</code>, return <em>the number of trailing zeroes in </em><code>n!</code>.</p> <p>Note that <code>n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 3 <strong>Output:</strong> 0 <strong...
0
{ "code": "class Solution {\npublic:\n int trailingZeroes(int n) \n {\n int count = 0;\n\n for (int i = 5; i <= n; i += 5)\n {\n int current_factor = i;\n\n while (current_factor % 5 == 0)\n {\n count++;\n current_factor /= 5;\n...
172
<p>Given an integer <code>n</code>, return <em>the number of trailing zeroes in </em><code>n!</code>.</p> <p>Note that <code>n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 3 <strong>Output:</strong> 0 <strong...
0
{ "code": "class Solution\n{\npublic:\n int trailingZeroes(int n)\n {\n int zeroCount = 0;\n while (n > 0)\n {\n n /= 5;\n zeroCount += n;\n }\n return zeroCount;\n }\n};", "memory": "7600" }
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "int prevPos[10001];\n\n\n\nclass Solution {\n\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i=0;\n int score = 0;\n int maxScore = 0;\n for(int j=0;j<10001;j++) prevPos[j]=-1;\n\n for(int j=0;j<nums.size();j++) {\n if(prevPos[nums[j]]<i) {\...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "int prevPos[10001];\n\n\n\nclass Solution {\n\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i=0;\n int score = 0;\n int maxScore = 0;\n std::fill(prevPos, prevPos+10001, -1);\n\n for(int j=0;j<nums.size();j++) {\n int &p=prevPos[nums[j]];\n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\n\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i=0;\n int score = 0;\n int maxScore = 0;\n int prevPos[10001] = {0};\n\n for(int j=0;j<nums.size();j++) {\n if(prevPos[nums[j]]<=i) {\n score += nums[j];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int idxs[10001] = {0};\n idxs[nums[0]] = 1;\n int last_idx = 0;\n\n int cur_sum = nums[0];\n int max_sum = 0;\n\n for (int i = 1; i < nums.size(); i++) {\n if (idxs[nums[i]] ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int l = 0,r = 0,ans = 0,sum = 0;\n vector<int> mp((*max_element(nums.begin(),nums.end()))+1);\n for(r = 0;r<nums.size();r++){\n while(l<=r && mp[nums[r]]==1){\n sum -= nums[l];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n // brute force - TC : O(N + N^2), SC : O(M)\n // int maxScore = 0;\n // int maxi = INT_MIN;\n // for(int it : nums){\n // maxi = max(maxi,it);\n // }\n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n vector<int> v(1e4+2);\n int i = 0, j = 0, n = nums.size(),sum = 0,ans = 0;\n while(i<n){\n sum += nums[i];\n v[nums[i]]++;\n while(v[nums[i]]==2){\n sum -= nu...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int j=0;\n int ans=0,sum=0;\n vector<int> arr(10001,-1);\n for(int i=0; i<nums.size();i++){\n if(arr[nums[i]]!=-1){\n while(j<=arr[nums[i]]){\n sum-=nums[...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n std::vector<int> sums;\n sums.resize(nums.size());\n int sum = 0;\n for (size_t i = 0; i < nums.size(); i++)\n {\n sum += nums[i];\n sums[i] = sum;\n }\n\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n // Calculate all the summs from first element to current\n std::vector<int> sums;\n sums.resize(nums.size());\n int sum = 0;\n for (size_t i = 0; i < nums.size(); i++)\n {\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n vector<int> sums(n, 0);\n vector<bool> exists(10001, false);\n int start = 0;\n int score = nums[0];\n int max_score = nums[0];\n\n sums[0] = nums[0];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n vector<int> p(n + 1, 0);\n for (int i = 0; i < n; i++) {\n p[i + 1] = p[i] + nums[i];\n }\n int s = 0;\n vector<int> m(10001, -1);\n int ans = 0...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "\nclass Solution {\npublic:\n\n \n int maximumUniqueSubarray(vector<int>& nums) {\n int n=nums.size(); \n vector<int> prefixSum(n+1,0);\n for (int i=0; i<n;i++){\n prefixSum[i+1]=prefixSum[i]+nums[i]; \n }\n vector<int> hashMap(10001,-1); \n int start=0;\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n int l = 0;\n int r = 0;\n unordered_map<int,int> mp;\n int maxSum = 0;\n int ans = 0;\n\n while(r < nums.size()) {\n mp[nums[r]]++;\n while(mp[nums[r]] > 1) {\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
0
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n int i = 0;\n int j = 0;\n int sum = 0;\n int maxsum = 0;\n unordered_map<int,int>mp;\n while(i<=j && j<n){\n if(mp.find(nums[j])==mp.end()){\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i=0,j=0;\n int res=0;\n int ans=0;\n unordered_map<int,int>m;\n while(j<=nums.size()-1)\n {\n \n m[nums[j]]++;\n // cout<<m[nums[j]]<<\" \"<<endl;\n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& s) {\n int maxVal = 0;\n map<int, int> mpp; //maps the no to freq\n int n = s.size();\n int l = 0;\n int r = 0;\n int sum = 0;\n while(r < n)\n {\n sum += s[r];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int j=0;\n int ans=0,sum=0;\n map<int,int> mp;\n for(int i=0; i<nums.size();i++){\n if(mp.find(nums[i])!=mp.end()){\n while(j<=mp[nums[i]]){\n sum-=nums[j...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int calculateSum(int start, int end, vector<int> & nums){\n int sum = 0;\n for(int i=start; i<end; i++){\n sum+=nums[i];\n }\n return sum;\n }\n int maximumUniqueSubarray(vector<int>& nums) {\n int start = 0, ans = 0, idx = ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int calculateSum(int start, int end, vector<int> & nums){\n int sum = 0;\n for(int i=start; i<end; i++){\n sum+=nums[i];\n }\n return sum;\n }\n int maximumUniqueSubarray(vector<int>& nums) {\n int start = 0, ans = 0, idx = ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n int l = 0;\n int r = 0;\n map<int,int> mp;\n int maxSum = 0;\n int ans = 0;\n\n while(r < nums.size()) {\n mp[nums[r]]++;\n while(mp[nums[r]] > 1) {\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n map<int, int> mp;\n int left =0;\n int sum =0;\n int maxSum =0;\n for(int i=0;i<nums.size();i++)\n {\n \n sum+=nums[i];\n mp[nums[i]]++;\n \n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n vector<int> prefixSum(n + 1, 0); // Prefix sum array\n unordered_map<int, int> indexMap; // To store the latest index of each element\n int ans = 0, start = 0;\n \n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n // Find unique elements subarray with maximum sum\n // start - starting index of substring\n // end - ending index of substing\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n int maxScore = 0;\n\n vector<int> prefixSum(n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
1
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n=nums.size();\n vector<int> presum(n,0);\n presum[0]=nums[0];\n\n for(int i=1; i<n; i++) presum[i]=nums[i]+presum[i-1];\n\n int maxi=0;\n int j=0;\n unordered_map<int,int> m...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n unordered_map<int,int> dict;\n queue<int> q;\n int currSum=0;\n int ans=0;\n for(int i=0;i<nums.size();i++){\n if(dict[nums[i]]==1){\n while(nums[i]!=q.front()){\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n map<int,int>mpp;\n vector<int> prefixSum(nums.size()+1);\n for(int i=1;i<nums.size()+1;i++){\n prefixSum[i]=prefixSum[i-1]+nums[i-1];\n }\n int sum=0;int l=0;\n for(int r=0;r...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n int ans=0,low=0,high=0;\n\n vector<int>sc(nums.size());\n sc[0]=nums[0];\n\n for(int i=1;i<nums.size();i++)\n {\n sc[i]= sc[i-1] +nums[i];\n }\n\n map<int, int>mp;\n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n int i = 0;\n int j=0; \n int ans = 0; \n int sz = nums.size();\n\n vector<int> pre(sz, 0); \n\n pre[0] = nums[0]; \n\n for(int i=1; i<nums.size(); i++) {\n pre[i]+= ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n std::vector<int> sums;\n sums.resize(nums.size());\n int sum = 0;\n for (size_t i = 0; i < nums.size(); i++)\n {\n sum += nums[i];\n sums[i] = sum;\n }\n\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) \n {\n int l=0, r=0; //define left and right pointers for sliding windows approach\n \n vector<int> sum_arr; //sum_arr stores sum of element of nums at index i;\n sum_arr.push_back(0); //initially l a...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n \n if(nums.size() == 0)\n {\n return 0;\n }\n if(nums.size() == 1)\n {\n return nums[0];\n }\n unordered_map<int, int> indices;\n unordered_ma...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n map<int,int>m;\n int sum=0;\n int temp=0;\n int currIndex=0;\n int pref=0;\n vector<int>prefix;\n\n for(int i=0; i<nums.size(); ++i){\n pref += nums[i];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n\n map<int,int>m;\n int sum=0;\n int temp=0;\n int currIndex=0;\n int pref=0;\n vector<int>prefix;\n\n for(int i=0; i<nums.size(); ++i){\n cout << temp <<\" \";\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n long l=0,r=0,n=nums.size(),sum=0,maxi=-1;\n unordered_map<int,int> mp;\n while(r<n){\n mp[nums[r]]++;\n sum += nums[r];\n while(mp[nums[r]] > 1){\n mp[nums[l]...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int sum=0;\n unordered_map<int,int>mp;\n int j=0,maxi=0;\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n mp[nums[i]]++;\n while(mp.size()!=(i-j+1))\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i = 0, sum = 0, ans = 0;\n unordered_map<int, int> m;\n for(int j = 0; j < nums.size(); j++){\n m[nums[j]]++;\n sum += nums[j];\n while(j-i+1 > m.size()){\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n unordered_set<int> set;\n set.insert(nums[0]);\n int s=0,e=0,mx_sum=nums[0], cur_sum = nums[0];\n\n for(int i=1; i<nums.size(); i++){\n if(!set.contains(nums[i])){\n e++;\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n std::unordered_set<int> s;\n int n = nums.size(), ans = 0, tot = 0, prev = -1, x, y;\n for (int i = 0; i < n; ++i) {\n x = nums[i];\n if (s.count(x)) {\n\n\t/**\n\tstd::vector<int> v(s.begin(), s.end());\n\tstd...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n std::unordered_set<int> s;\n int n = nums.size(), ans = 0, tot = 0, prev = -1, x, y;\n for (int i = 0; i < n; ++i) {\n x = nums[i];\n if (s.count(x)) {\n\n\t/**\n\tstd::vector<int> v(s.begin(), s.end());\n\tstd...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int l = 0, r = 0;\n int n = nums.size();\n vector<int> m(100001, 0); // Assuming nums contains values between 0 and 100000\n int sum = 0;\n int maxs = 0;\n\n while (r < n) {\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n //Candidate - keeps track of highest sum\n int candidateSum = nums[0];\n //Prefix array\n vector<int> prefix(nums.size());\n prefix[0] = nums[0];\n //Unique values set\n unordere...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n vector<int> prefix(nums.size(),0);\n prefix[0] = nums[0];\n for(int i=1;i<nums.size();i++){\n prefix[i] = prefix[i-1]+nums[i]; \n }\n int ans = INT_MIN;\n unordered_set<int> ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n vector<int> presum(n);\n presum[0] = nums[0];\n for (int i=1;i<n;i++) presum[i] = presum[i-1]+nums[i];\n\n int i=0, j, res=0, cnt=0;\n unordered_map<int, int> map...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int left=0,ans=0;\n //Create prefix sum array\n vector<int> prefix_sum={nums[0]};\n for(int i=1; i<nums.size();i++){\n prefix_sum.push_back(prefix_sum.back()+nums[i]);\n } \n unordered...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int ans=-1e9;\n set<int> st(nums.begin(),nums.end());\n int k=st.size();\n int left=0;\n int right=0;\n int n=nums.size();\n int sum=0;\n unordered_map<int,int> mp;\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n= nums.size();\n unordered_map<int, int>mp;\n int sum=0;\n int j=0;\n int i=0;\n int maxi=0;\n while(i<n ){\n if(mp.find(nums[i])==mp.end()){\n mp[n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int left = 0;\n int right = 0;\n unordered_map<int,int> mpp;\n int sum = 0;\n int ans = 0;\n while(right < nums.size()){\n if(mpp.find(nums[right]) != mpp.end()){\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
2
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int j=0;\n int count=0;\n int maxi=0;\n unordered_set<int>st;\n for(int i=0;i<nums.size();i++){\n while(st.find(nums[i])!=st.end()){\n st.erase(nums[j]);\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n=nums.size();\n vector<int> presum(n,0);\n presum[0]=nums[0];\n\n for(int i=1; i<n; i++) presum[i]=nums[i]+presum[i-1];\n\n int maxi=0;\n int j=0;\n unordered_map<int,int> m...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int i=0,j=0;\n vector<int> res(nums.size());\n unordered_set<int> st;\n int sum = 0;\n for(int i=0; i<nums.size(); i++) {\n sum += nums[i];\n res[i] = sum;\n }\n...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int sum =0;\n int current =0;\n set<int>st;\n int first =0;\n int last =0;\n while (first<=last && last<nums.size()){\n if (st.find(nums[last])==st.end()){\n s...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n set<int>val;\n int i=0;\n int maxval=0;\n int ans=0;\n for(int j=0;j<nums.size();j++) {\n auto it1=val.find(nums[j]);\n if(it1 == val.end()) {\n val.insert...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n set<int> s;\n int start=0;\n int su=0;\n int maxi=0;\n for(int i=0;i<nums.size();i++)\n {\n if(s.find(nums[i])==s.end())\n {\n su+=nums[i];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n \n\n //requirements \n //find the longest length with unique char \n //return the sum of of all values\n //it can be solved using sliding window\n\n int i=0;\n int j=0;\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n=nums.size();\n map<int,int>mp;\n int j=0;\n int sum=0;\n int ans=-1;\n for(int i=0;i<n;++i){\n mp[nums[i]]++;\n sum+=nums[i];\n while(j<i && mp[nu...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n \n int n = nums.size();\n int sum = 0;\n int ans = 0;\n map<int,int> mp;\n int i = 0, j = 0;\n while(j<nums.size()){\n mp[nums[j]]++;\n sum+=nums[j];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int l = 0;\n int score = 0;\n int res = 0;\n map<int,int> mp;\n for (int r=0;r<nums.size();r++){\n mp[nums[r]]++;\n score += nums[r];\n while(r-l+1!=mp.size())...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int sum = 0 ;\n map<int,int> m ;\n int count = 0 ;\n int i=0 ; \n int j=0 ; \n int maxi = 0 ;\n while(j<nums.size()){\n sum = sum + nums[j] ;\n m[nums[j]]++...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n = nums.size();\n set<int> st;\n vector<int> cnt(10001,0);\n int j = 0;\n int sum = 0;\n int res = INT_MIN;\n for(int i = 0;i<n;i++)\n {\n sum += nums[i];\...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int sum = 0;\n int maxSum = 0;\n unordered_set<int> mp;\n vector<int> temp = {nums[0]};\n for(int i = 1; i < nums.size(); i++){\n temp.push_back(temp[i-1]+nums[i]);\n }\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int sum = 0;\n int maxSum = 0;\n unordered_set<int> mp;\n vector<int> temp = {nums[0]};\n for(int i = 1; i < nums.size(); i++){\n temp.push_back(temp[i-1]+nums[i]);\n }\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int l = 0, r = 0,ans = 0;\n int n = nums.size();\n vector<int> prefix(n);\n long long curr = 0;\n for(int i=0;i<n;i++){\n curr += nums[i];\n prefix[i] = curr;\n }\...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int size = nums.size();\n unordered_map<int,bool>visit;\n for(int i=0;i<size;i++){\n visit[i] = false;\n }\n int score = 0, first = 0, second = 0, a = 0;\n while(second < siz...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int size = nums.size();\n unordered_map<int,bool>visit;\n for(int i=0;i<size;i++){\n visit[i] = false;\n }\n int score = 0, first = 0, second = 0, a = 0;\n while(second < siz...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& v) {\n int i = 0, j = 0, n = v.size();\n int ans = 0, sum = 0;\n set<int> s;\n while (j < n) {\n if (s.count(v[j])) {\n while (s.count(v[j])) {\n sum -= v[i];\n ...
1,813
<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p> <p>Return <em>the <strong>maximum score</strong> you can g...
3
{ "code": "class Solution {\npublic:\n int maximumUniqueSubarray(vector<int>& nums) {\n int n=nums.size(),l=0,r=0,sum=0,ans=0;\n set<int> s;\n for(r;r<n;r++){\n while(s.find(nums[r])!=s.end()){\n s.erase(nums[l]);\n sum-=nums[l];\n l++;\n...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size();\n int total_ans = 0;\n\n if(n==1) {\n return 0;\n }\n\n int curr_diff = nums[1] - nums[0];\n\n for(int i=1;i<n;) {\n int diff = nums[i] - n...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size();\n if (n < 3) return 0; \n\n int total_slices = 0;\n int current_length = 0; \n\n for (int i = 2; i < n; i++) {\n // Check if nums[i], \n if (num...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) \n {\n if(nums.size()<3)\n {\n return 0;\n }\n vector<int>dp(nums.size(),0);\n for(int i=2;i<nums.size();i++)\n {\n if(nums[i]-nums[i-1]==nums[i-1]-nums[i-2])\n ...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size(), ans = 0, diff = INT_MAX, count = 0;\n for(int i=1;i<n;i++) {\n if(nums[i] - nums[i-1] == diff) {\n count++;\n if(count > 2) {\n a...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n\n int n = nums.size();\n vector<int> dp_cnt(n, 0);// arithmetic cnt end at index i\n vector<int> dp_diff(n, 0);// diff with previous element\n\n dp_cnt[0] = 0;\n dp_diff[0] = 0;\n\n ...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size();\n if(n < 3) return 0;\n int diff = nums[1] - nums[0], cnt = 2, ans = 0;\n for(int i = 2;i < n;i++){\n if(diff == nums[i]-nums[i-1]) cnt++;\n else{\n ...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
0
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size();\n if(n < 3) return 0;\n vector<int> dp(n,0); // no of arithmetic subarrays ending before or at index i;\n\n // dp[2] = \n nums[1] - nums[0] == nums[2] -nums[1] ? dp[2] ...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
3
{ "code": "class Solution {\npublic:\n int func(int curr, int prev, int count, int diff, vector<int> &nums){\n int ans = 0;\n if(curr == nums.size()){\n if(count >= 3)return 1;\n return 0;\n }\n if(prev != -1 && curr-prev == 1 && diff == INT_MIN){\n ans ...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
3
{ "code": "class Solution {\n vector<int> dp;\n int count = 0;\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n = nums.size()-1;\n dp = vector<int>(n+1,-1);\n helper(nums, n);\n return count;\n }\n\n int helper(vector<int>&nums, int n){\n if(n < 2)...
413
<p>An integer array is called arithmetic if it consists of <strong>at least three elements</strong> and if the difference between any two consecutive elements is the same.</p> <ul> <li>For example, <code>[1,3,5,7,9]</code>, <code>[7,7,7,7]</code>, and <code>[3,-1,-5,-9]</code> are arithmetic sequences.</li> </ul> <p...
3
{ "code": "class Solution {\npublic:\n int numberOfArithmeticSlices(vector<int>& nums) {\n int n=nums.size();\n vector<int>dp(5002,0);\n for(int i=2;i<n;i++){\n if((nums[i]-nums[i-1])==(nums[i-1]-nums[i-2])){\n dp[i]=dp[i-1]+1;\n }\n }\n int an...
414
<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,2...
0
{ "code": "class Solution {\npublic:\n int thirdMax(vector<int>& nums) {\n long long largest =LONG_MIN;\n long long second =LONG_MIN;\n long long third = LONG_MIN;\n\n for(int i =0;i<nums.size();i++){\n if(largest==nums[i] || second == nums[i]||third == nums[i]){\n ...
414
<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,2...
0
{ "code": "class Solution {\npublic:\n int thirdMax(vector<int>& nums) {\n int n = nums.size();\n long long int maxi = LONG_MIN;\n long long int smaxi = LONG_MIN;\n long long int tmaxi = LONG_MIN;\n for(int i=0; i<n; i++) {\n if(nums[i] > maxi) {\n tmaxi...
414
<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,2...
0
{ "code": "class Solution {\npublic:\n int thirdMax(vector<int>& nums) {\n \n int n= nums.size();\n \n pair<int, int>fm= {INT_MIN, -1};\n pair<int, int>sm= {INT_MIN, -1};\n pair<int, int>tm= {INT_MIN, -1};\n \n for(int i=0; i<n; i++)\n {\n i...
414
<p>Given an integer array <code>nums</code>, return <em>the <strong>third distinct maximum</strong> number in this array. If the third maximum does not exist, return the <strong>maximum</strong> number</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,2...
0
{ "code": "class Solution {\npublic:\n int thirdMax(vector<int>& nums) {\n \n int n= nums.size();\n \n pair<int, int>fm= {INT_MIN, -1};\n pair<int, int>sm= {INT_MIN, -1};\n pair<int, int>tm= {INT_MIN, -1};\n \n for(int i=0; i<n; i++)\n {\n i...