You are given an array arr of N integers. For each index i, you are required to determine the number of contiguous subarrays that fulfill the following conditions:
The value at index i must be the maximum element in the contiguous subarrays, and
These contiguous subarrays must either start from or end on index i.
Signature
int[] countSubarrays(int[] arr)
Input
Array arr is a non-empty list of unique integers that range between 1 to 1,000,000,000
Size N is between 1 and 1,000,000
Output
An array where each index i contains an integer denoting the maximum number of contiguous subarrays of arr[i]
Example:
arr = [3, 4, 1, 6, 2]
output = [1, 3, 1, 5, 1]
Explanation:
For index 0 - [3] is the only contiguous subarray that starts (or ends) with 3, and the maximum value in this subarray is 3.
For index 1 - [4], [3, 4], [4, 1]
For index 2 - [1]
For index 3 - [6], [6, 2], [1, 6], [4, 1, 6], [3, 4, 1, 6]
For index 4 - [2]
So, the answer for the above input is [1, 3, 1, 5, 1]
Solution :
function countSubarrays(arr) {
let res = [];
for(let i=0 ; i< arr.length; i++) {
let leftCount = 0;
let rightCount= 0;
for(let j= i-1 ; j >=0 ; j--) {
if(arr[i] >= arr[j]) {
leftCount++;
} else {
break;
}
}
for(let k= i+1 ; k < arr.length ; k++) {
if(arr[i] >= arr[k]) {
rightCount++;
} else {
break;
}
}
res.push(leftCount + rightCount + 1);
}
return res;
}Stack Solution (o(n))
function countSubarrays(arr) {
let stack = [];
const ans = Array(arr.length).fill(0); // left-right possibility
for (let i = 0; i < arr.length; i++) {
while (stack.length && arr[stack[stack.length - 1]] < arr[i]) {
ans[i] += ans[stack.pop()];
}
stack.push(i);
ans[i]++;
}
stack = [];
const right = Array(arr.length).fill(0); // right-to -left passibility
for (let i = arr.length - 1; i >= 0; i--) {
while (stack.length && arr[stack[stack.length - 1]] < arr[i]) {
let idx = stack.pop();
ans[i] += right[idx];
right[i] += right[idx];
}
stack.push(i);
right[i]++;
}
return ans;
}