saurav_kalsoor

Top K Students - KOTLIN

Nov 23rd, 2021 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.94 KB | None | 0 0
  1. import java.util.*
  2.  
  3. var sc: Scanner = Scanner(System.`in`)
  4.  
  5. fun main() {
  6.     val n: Int = sc.nextInt()
  7.     val k: Int = sc.nextInt()
  8.     val points = Array(n) { IntArray(4) }
  9.     for (i in 0 until n) {
  10.         for (j in 0..3) {
  11.             points[i][j] = sc.nextInt()
  12.         }
  13.     }
  14.     topKStudents(n, k, points)
  15. }
  16.  
  17. fun topKStudents(n: Int, k: Int, points: Array<IntArray>) {
  18.     val totalPoints = IntArray(n)
  19.     for (i in 0 until n) {
  20.         totalPoints[i] = 0
  21.         for (j in 0..3) {
  22.             totalPoints[i] += points[i][j]
  23.         }
  24.     }
  25.     val temp = IntArray(n)
  26.    
  27.     for (i in 0 until n)
  28.         temp[i] = totalPoints[i]
  29.  
  30.     temp.sortDescending()
  31.    
  32.     val topK: ArrayList<Int> = ArrayList<Int>()
  33.     for (i in 0 until n) {
  34.         if (totalPoints[i] + 100 >= temp[k - 1])
  35.             topK.add(i)
  36.     }
  37.     println(topK.size)
  38.     for (index in topK) {
  39.         print("$index ")
  40.     }
  41.     println()
  42. }
  43.  
Add Comment
Please, Sign In to add comment