Advertisement
saurav_kalsoor

Top K Students - JAVA

Nov 23rd, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Test {
  4.  
  5.     static Scanner sc = new Scanner(System.in);
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         int n = sc.nextInt();
  10.         int k = sc.nextInt();
  11.         int points[][] = new int[n][4];
  12.  
  13.         for(int i = 0; i < n; i++){
  14.             for(int j = 0; j < 4; j++){
  15.                 points[i][j] = sc.nextInt();
  16.             }
  17.         }
  18.  
  19.         topKStudents(n, k, points);
  20.  
  21.     }
  22.  
  23.     public static void topKStudents(int n, int k, int[][] points) {
  24.         int totalPoints[] = new int[n];
  25.         for(int i = 0; i < n; i++){
  26.             totalPoints[i] = 0;
  27.             for(int j = 0; j < 4; j++){
  28.                 totalPoints[i] += points[i][j];
  29.             }
  30.         }
  31.  
  32.         Integer temp[] = new Integer[n];
  33.         for(int i = 0; i < n; i++)
  34.             temp[i] = totalPoints[i];
  35.  
  36.         Arrays.sort(temp, Collections.reverseOrder());
  37.  
  38.         ArrayList<Integer> topK = new ArrayList<Integer>();
  39.  
  40.         for(int i=0; i < n; i++){
  41.             if(totalPoints[i] + 100 >= temp[k-1])
  42.                 topK.add(i);
  43.         }
  44.  
  45.         System.out.println(topK.size());
  46.         for(int index : topK){
  47.             System.out.print(index + " ");
  48.         }
  49.         System.out.println();
  50.     }
  51. }
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement