forked from noodles-sed/Simple-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakearrayZero.cpp
More file actions
39 lines (33 loc) · 973 Bytes
/
MakearrayZero.cpp
File metadata and controls
39 lines (33 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public:
int countValidSelections(vector<int>& nums) {
int n = nums.size();
int count = 0;
for (int i = 0; i < n; i++) {
if (nums[i] != 0) continue;
for (int dir = -1; dir <= 1; dir += 2) {
vector<int> arr = nums;
int curr = i;
int d = dir;
while (curr >= 0 && curr < n) {
if (arr[curr] == 0) {
curr += d;
} else {
arr[curr]--;
d = -d;
curr += d;
}
}
bool allZero = true;
for (int x : arr) {
if (x != 0) {
allZero = false;
break;
}
}
if (allZero) count++;
}
}
return count;
}
};