commit 14bc4f624ae5c1068949840eb69893cb1ac80e19
parent 49bee6fe4ccb5116cbbbc7623933d51e11e55e25
Author: Jack Sangdahl <jack@pngu.org>
Date: Mon, 27 Oct 2025 21:25:50 -0600
3354. make array elements equal to zero
Diffstat:
2 files changed, 77 insertions(+), 0 deletions(-)
diff --git a/make_arr_elem_zero.c b/make_arr_elem_zero.c
@@ -0,0 +1,37 @@
+// 3354. Make Array Elements Equal to Zero
+#include "leetcode.h"
+
+/*
+ * you are given an integer array 'nums'. start by selecting a starting position
+ * 'curr' such that 'nums[curr] == 0' and choose a movement direction of either
+ * left or right. after that, you repeat the following process: if 'curr' is out
+ * of the range '[0, n - 1]', this process ends. if 'nums[curr] == 0', move in
+ * the current direction by incrementing 'curr' if you are moving right, or
+ * decrmenting 'curr' if you are moving left. else if 'nums[curr] > 0',
+ * decrement 'nums[curr] by 1', reverse your movement direction, take a step in
+ * the new direction. a selection of the initial position 'curr' and movement
+ * direction is considered valid if every element in 'nums' becomes 0 by the end
+ * of the process. return the number of possible valid sections.
+ */
+
+int countValidSelections(int *nums, int numsSize) {
+ int sum = 0, ans = 0, curr = 0;
+ for (int i = 0; i < numsSize; i++)
+ sum += nums[i];
+ for (int i = 0; i < numsSize; i++) {
+ curr += nums[i];
+ if (!nums[i]) {
+ if (curr * 2 == sum)
+ ans += 2;
+ else if (abs(sum - 2 * curr) == 1)
+ ans++;
+ }
+ }
+ return ans;
+}
+
+int main() {
+ int n1[] = {1, 0, 2, 0, 3}, n2[] = {2, 3, 4, 0, 4, 1, 0};
+ printf("%d\n", countValidSelections(n1, ARRAY_SIZE(n1))); // expect: 2
+ printf("%d\n", countValidSelections(n2, ARRAY_SIZE(n2))); // expect: 0
+}
diff --git a/make_arr_elem_zero.py b/make_arr_elem_zero.py
@@ -0,0 +1,40 @@
+# 3354. Make Array Elements Equal to Zero
+from itertools import accumulate
+
+"""
+you are given an integer array 'nums'. start by selecting a starting position
+'curr' such that 'nums[curr] == 0' and choose a movement direction of either
+left or right. after that, you repeat the following process: if 'curr' is out
+of the range '[0, n - 1]', this process ends. if 'nums[curr] == 0', move in
+the current direction by incrementing 'curr' if you are moving right, or
+decrmenting 'curr' if you are moving left. else if 'nums[curr] > 0',
+decrement 'nums[curr] by 1', reverse your movement direction, take a step in
+the new direction. a selection of the initial position 'curr' and movement
+direction is considered valid if every element in 'nums' becomes 0 by the end
+of the process. return the number of possible valid sections.
+"""
+
+
+class Solution(object):
+ def countValidSelections(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ prefix = list(accumulate(nums, initial=0))
+ ans, s = 0, prefix[-1]
+ for i, j in zip(nums, prefix):
+ j *= 2
+ if i != 0:
+ continue
+ if s == j:
+ ans += 2
+ if abs(s - j) == 1:
+ ans += 1
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.countValidSelections(nums=[1, 0, 2, 0, 3]))
+ print(obj.countValidSelections(nums=[2, 3, 4, 0, 4, 1, 0]))