Skip to content

Instantly share code, notes, and snippets.

@kenneropia
Last active January 28, 2023 15:16
Show Gist options
  • Save kenneropia/a1841c492ed061fdf5b672b9e1e2ad72 to your computer and use it in GitHub Desktop.
Save kenneropia/a1841c492ed061fdf5b672b9e1e2ad72 to your computer and use it in GitHub Desktop.
function isMonotonic(arr: Number[]): Boolean {
let monoLeft = true;
let monoRight = true;
// loop thru the arr and check for the side by side items that makes the array non-monotonic
for (let i = 0; i < arr.length - 1; i++) {
// To check if
// array is not increasing
if (arr[i] > arr[i + 1]) {
monoLeft = false;
}
if (arr[i] < arr[i + 1]) {
monoRight = false;
}
}
// Pick one whether left or right
return monoLeft || monoRight;
}
function rotateArr(arr: Number[], step: number): Number[] {
// I could have mutated the original arr too
const outputArr = [...arr];
//loop throught the array and push the rotated items into it
for (let i = 0; i < step; i++) {
outputArr.push(outputArr[i]);
}
//delete the stale array items
for (let j = 0; j < step; j++) {
outputArr.shift();
}
return outputArr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment