Created
May 11, 2020 14:28
-
-
Save tandavala/220e398c0eeab920ad7ea04e321b3b46 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 print3largest(array, size){ | |
let i, first, second, third; | |
if(size < 3){ | |
throw new Error("Invalid Input"); | |
return; | |
} | |
third = first = second = Number.MIN_VALUE; | |
for(i =0; i < size; i++){ | |
// If current element is greater than frist | |
if(array[i] > first){ | |
third = second; | |
second = first; | |
first = array[i]; | |
} | |
// if array[i] is in between first and second then update second | |
else if(array[i] > second){ | |
third = second; | |
second = array[i]; | |
} | |
else if(array[i] > third){ | |
third = array[i]; | |
} | |
} | |
console.log(` ${first}, ${second}, ${third}`); | |
} | |
let array = [12, 2, 43, 1000, 7, 55, 65, 100]; | |
console.log(array) | |
print3largest(array, 8); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment