Skip to content

Commit 89276ad

Browse files
committed
Time: 1 ms (100.00%), Space: 41.9 MB (100.00%) - LeetHub
1 parent 2745a3b commit 89276ad

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int countValidSelections(int[] nums) {
3+
int cnt =0;
4+
int n = nums.length;
5+
for(int i=0;i<n;i++){
6+
if(nums[i]==0){
7+
int lfs = sum(0,i,nums);
8+
int rfs = sum(i+1,n,nums);
9+
if(lfs == rfs ){
10+
cnt+=2;
11+
}else if(Math.abs(lfs-rfs)==1){
12+
cnt+=1;
13+
}
14+
}
15+
}
16+
return cnt ;
17+
}
18+
19+
public int sum(int start , int end , int[] nums){
20+
int ans =0;
21+
for(int i= start ; i<end;i++){
22+
ans +=nums[i];
23+
}
24+
return ans ;
25+
}
26+
}

0 commit comments

Comments
 (0)