Algo friday 3 solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const startAndEnd = (nums, val) => { | |
if (!Array.isArray(nums) || typeof val !== 'number') throw new Error('Invalid input') | |
const min = Math.min(...nums); | |
let positions = [-1, -1] | |
let countLessThanVal = 0 | |
let countOfVal = 0 | |
let countOfMin = 0 | |
for (let num of nums) { | |
if (num === min) countOfMin++ | |
if (num < val) countLessThanVal++ | |
if (num === val) countOfVal++ | |
} | |
if (!countOfVal) return positions | |
positions[0] = val === min ? 0 : countLessThanVal + countOfMin - 1 | |
positions[1] = positions[0] + countOfVal - 1 | |
return positions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.