Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active June 2, 2025 16:20
Show Gist options
  • Save tatsuyax25/a0ac2964256dcd18d65cd4ad6fd4244f to your computer and use it in GitHub Desktop.
Save tatsuyax25/a0ac2964256dcd18d65cd4ad6fd4244f to your computer and use it in GitHub Desktop.
You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri]. For each queries[i]: Select a subset of indices within the range [li, ri] in nums. Decrement the values at the selected indices by 1. A Zero Array
/**
* @param {number[]} nums
* @param {number[][]} queries
* @return {boolean}
*/
var isZeroArray = function(nums, queries) {
// Create a difference array initialized with zeros
let diff = new Array(nums.length + 1).fill(0);
// Apply range updates using the difference array
for (let [start, end] of queries) {
diff[start] += 1; // Increment at the start index
diff[end + 1] -= 1; // Decrement right after the end index
}
let cumulativeSum = 0; // Variable to track cumulative sum
for (let i = 0; i < nums.length; i++) {
cumulativeSum += diff[i]; // Update cumulative sum
// If cumulativeSum is less than the corresponding nums value, return false
if (cumulativeSum < nums[i]) return false;
}
return true; // If all conditions are met, return true
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment