Skip to content

Instantly share code, notes, and snippets.

@chygoz2
Last active April 27, 2021 13:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chygoz2/277e3ba4f04e9b9e82d640dec8174f1d to your computer and use it in GitHub Desktop.
Save chygoz2/277e3ba4f04e9b9e82d640dec8174f1d to your computer and use it in GitHub Desktop.
Algo friday 3 solution
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
}
@chygoz2
Copy link
Author

chygoz2 commented Apr 27, 2021

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment