Skip to content

Instantly share code, notes, and snippets.

@remi-bruguier
Last active May 18, 2020 06:47
Show Gist options
  • Save remi-bruguier/093757e835e18670888b36988e6371b6 to your computer and use it in GitHub Desktop.
Save remi-bruguier/093757e835e18670888b36988e6371b6 to your computer and use it in GitHub Desktop.
You are given an array of integers in an arbitrary order. Return whether or not it is possible to make the array non-decreasing by modifying at most 1 element to any value.
const couldBeMadeNonDecreasing = (arr:number[]):Boolean => {
let decreasingPairs = 0
for(let i in arr){
if(arr[i] > arr[+i+1]){
decreasingPairs++
}
}
return decreasingPairs <= 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment