Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1131ab9

Browse files
Create Longest subset zero sum
1 parent 4e08b9d commit 1131ab9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎Hashmaps/Longest subset zero sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include<unordered_map>
2+
int lengthOfLongestSubsetWithZeroSum(int* arr, int n) {
3+
unordered_map<int,int> ourmap;
4+
int maxlength=0;
5+
int sum=0;
6+
for(int i=0;i<n;i++){
7+
sum=sum+arr[i];
8+
if(arr[i]==0&&maxlength==0){
9+
maxlength=1;
10+
}
11+
if(sum==0&&(i+1>maxlength)){
12+
maxlength=i+1;
13+
}
14+
if(ourmap.count(sum)>0){
15+
maxlength=max(maxlength,i-ourmap[sum]);
16+
}
17+
else{
18+
ourmap[sum]=i;
19+
}
20+
}
21+
return maxlength;
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /