Skip to content

Instantly share code, notes, and snippets.

@mpelzsherman
Created February 25, 2021 01:29
Show Gist options
  • Save mpelzsherman/ba1755649406acc5986b9089a8f73bc4 to your computer and use it in GitHub Desktop.
Save mpelzsherman/ba1755649406acc5986b9089a8f73bc4 to your computer and use it in GitHub Desktop.
function countSubarrays(arr) {
var result = [];
for (var index = 0; index < arr.length; index++) {
var count = 1; // always include the index itself
// look forward
var subIndex = index + 1;
while(arr[subIndex] < arr[index] && subIndex < arr.length) {
count++;
subIndex++;
}
// look backward
var subIndex = index - 1;
while(arr[subIndex] < arr[index] && subIndex >= 0) {
count++;
subIndex--;
}
result[index] = count;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment