Created
June 2, 2019 20:35
-
-
Save anushshukla/fec895749fb1eacfa2b8fb0cf900eaa2 to your computer and use it in GitHub Desktop.
Longest Whole Number Integer Sequence regardless of continuity.
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
function LongestIncreasingSequence(arrayOfNaturaNumbers) { | |
let lis = 1; | |
let lastNumber; | |
const totalNumbers = arrayOfNaturaNumbers.length; | |
let internalLoop = totalNumbers - 1; | |
const lisFound = lis > lis; | |
const sequenceRecords = []; | |
for (let index = 0; index < totalNumbers - 1 && !lisFound; index++) { | |
// if (index > 0) break; // testing | |
const currNumber = arrayOfNaturaNumbers[index]; | |
// @todo below LOC: to skip looping if same consecutive number, currently it breaks the code due to missing index in sequenceRecords | |
// if (lastNumber === currNumber) continue; | |
if (internalLoop < lis) break; | |
let currLis = 1; | |
lastNumber = currNumber; | |
let lastHighestNumber = currNumber; | |
sequenceRecords.push([{ number: currNumber, lis: 1, noSequence: false }]); | |
for (let i = 0; i < internalLoop; i++) { | |
let noSequence = false; | |
const internalIndex = index + i + 1; | |
const nextNumber = arrayOfNaturaNumbers[internalIndex]; | |
const isGreater = nextNumber > lastHighestNumber; | |
const anotherSequencePossibile = nextNumber - currNumber < lastHighestNumber - currNumber; | |
// console.info('lis calc', 'currNumber:', currNumber, 'nextNumber:', nextNumber, lastHighestNumber, currLis, isGreater, anotherSequencePossibile) | |
if (isGreater) { | |
currLis += 1; | |
lastHighestNumber = nextNumber; | |
} | |
let currLisBeforeUpdate = currLis; | |
let prevHighestNumber = lastHighestNumber; | |
if (anotherSequencePossibile) { | |
// console.info('reverse check from', nextNumber); | |
for (let j = internalIndex - 1; j > -1; j--) { | |
const diff = nextNumber - arrayOfNaturaNumbers[j]; | |
const sequenceRecord = sequenceRecords[index][j] || { lis: 1, noSequence: false }; | |
// console.info('reverse check', arrayOfNaturaNumbers[j], 'diff', diff, 'lis', sequenceRecord); | |
if (diff > 0 && !sequenceRecord.noSequence) { | |
prevHighestNumber = lastHighestNumber; | |
lastHighestNumber = nextNumber; | |
currLis = sequenceRecord.lis + 1; | |
break; | |
} | |
if (j === 0) noSequence = true; | |
currLis = sequenceRecord.lis; | |
} | |
} | |
sequenceRecords[index][internalIndex] = { number: nextNumber, lis: currLis, noSequence }; | |
// console.info('currLisBeforeUpdate', currLisBeforeUpdate, 'currLis', currLis); | |
if (currLisBeforeUpdate > currLis) { | |
currLis = currLisBeforeUpdate; | |
lastHighestNumber = prevHighestNumber; | |
} | |
} | |
internalLoop--; | |
if (currLis > lis) lis = currLis; | |
} | |
// console.info('sequenceRecords', sequenceRecords); | |
return lis; | |
} | |
// Sample test array with expected output | |
// [1, 2, 3, 0, 0, 16, 4, 5, 17, 9, 10] -> 7 | |
// [11, 12, 13, 9, 1, 2, 3, 5, 0, 6, 21] -> 6 | |
// [9, 9, 4, 2] -> 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment