a new method has to be derived for decrypting secret codes. there is a secret array arr of length n, along with a 2d array query with dimension m*2, where each row denotes a pair (l, r) which denotes the sum arr[l] + arr[l+1] + …. + arr[r]. the goal is to determine all the indices of arr whose value can be identified. eg. n = 4 query = [[1, 3], [1, 2], [4, 4]] first query gives the value of arr[1] + arr[2] + arr[3]. the second query gives the value of arr[1]+ arr[2]. third query gives the value of arr[4] directly. using the above only index 3 and 4 can be determined (1 based indexing). so ans = [3, 4]. constraints: 1 <= n <= 1e5; 1 <= m <= 1e5; 1 <= l <= r <= n
Tried the below code but failed didnt getting the correct output for some cases.
My Code:
function findIndices(n, queries) {
const arr = new Array(n).fill(0);
const indices = [];
for (const [l, r] of queries) {
arr[l - 1]++;
if (r < n) arr[r]--;
}
for (let i = 1; i < n; i++) {
arr[i] += arr[i - 1];
}
for (let i = 0; i < n; i++) {
if (arr[i] > 0) {
indices.push(i + 1);
}
}
return indices;
}