Max Min Discussions | | HackerRank

Max Min

  • + 0 comments

    Kotlin

    fun maxMin(k: Int, arr: Array): Int { var sortedArray = arr.sorted() var result: Int? = null

    for(i in 0 until sortedArray.size - k - 1) {
        var diff = sortedArray[i + k - 1] - sortedArray[i]
        if(result == null) {
            result = diff
        } else {
            if(result > diff) {
                result = diff
            }
        }
    }
    return result ?: 0
    

    }