Write a program to find top two maximum numbers in a array with kotlin

Write a program to find top two maximum numbers in the given array. You should not use any sorting functions.

You should iterate the array only once. You should not use any kind of collections in java.

Kotlin program to find top two maximum numbers in a array


fun printTwoMaxNumbers(nums: IntArray) {
    var maxOne = 0
    var maxTwo = 0
    for (n in nums) {
        if (maxOne < n) {
            maxTwo = maxOne
            maxOne = n
        } else if (maxTwo < n) {
            maxTwo = n
        }
    }
    println("First Max Number: $maxOne")
    println("Second Max Number: $maxTwo")
}

fun main(a: Array<String>) {
    val num = intArrayOf(5, 34, 78, 2, 45, 1, 99, 23)
    printTwoMaxNumbers(num)
}


Output


First Max Number: 99
Second Max Number: 78

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions