Skip to content

Instantly share code, notes, and snippets.

@karansapolia
Created January 3, 2019 21:42
Show Gist options
  • Save karansapolia/37cea366fa6957cb6159d35c4f097f26 to your computer and use it in GitHub Desktop.
Save karansapolia/37cea366fa6957cb6159d35c4f097f26 to your computer and use it in GitHub Desktop.
const inputArr = JSON.parse(process.argv[2]);
console.log(process.argv);
console.log(inputArr);
let midPoint = 0;
if(inputArr.length > 0) {
if(inputArr.length % 2 == 0) {
midPoint = (inputArr.length / 2) - 1;
}
else {
midPoint = (inputArr.length - 1) / 2;
console.log( inputArr.length, (inputArr.length - 1)/2, midPoint);
}
}
let PeaksArray = [];
addToPeaks(midPoint);
console.log("The peaks are : ", PeaksArray);
function addToPeaks(midPoint) {
if(midPoint == 0) {
if(inputArr[0] >= inputArr[1]) {
PeaksArray.push(inputArr[0]);
}
}
else if(midPoint == inputArr.length - 1) {
if(inputArr[inputArr.length - 1] >= inputArr[inputArr.length - 2]);
PeaksArray.push(inputArr[inputArr.length - 1]);
}
else if(inputArr[midPoint] >= inputArr[midPoint + 1] && inputArr[midPoint] >= inputArr[midPoint - 1]) {
PeaksArray.push(inputArr[midPoint]);
addToPeaks(midPoint = midPoint + Math.floor(midPoint / 2));
//addToPeaks(Math.floor(midPoint / 2));
}
else if ( inputArr[midPoint] < inputArr[midPoint + 1] ) {
midPoint = midPoint + Math.floor(midPoint / 2);
addToPeaks(midPoint);
}
else if( inputArr[midPoint] < inputArr[midPoint - 1]) {
midPoint = Math.floor(midPoint / 2);
addToPeaks(midPoint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment