|
| 1 | +/* |
| 2 | +There are N players with IDs from 1 to N, who are participating in the Javelin throw competition which has two rounds. The first is the qualification round, followed by the final round. The qualification round has gotten over, and you are given the longest distance that each of the N players has thrown as A1,A2,…,AN. Now, the selection process for the final round happens in the following two steps: |
| 3 | +
|
| 4 | +If the longest throw of a player in the qualification round is greater than or equal to the qualification mark of M cm, they qualify for the final round. |
| 5 | +
|
| 6 | +If after step 1, less than X players have qualified for the finals, the remaining spots are filled by players who have thrown the maximum distance among the players who have not qualified yet. |
| 7 | +
|
| 8 | +You are given the best throws of the N players in the qualification round A1,A2,…,AN and the integers M and X. Print the list of the players who will qualify for the finals in increasing order of their IDs. |
| 9 | +
|
| 10 | +Sample Input 1 |
| 11 | +3 |
| 12 | +3 8000 2 |
| 13 | +5000 5001 5002 |
| 14 | +3 5000 2 |
| 15 | +7999 7998 8000 |
| 16 | +4 6000 3 |
| 17 | +5999 5998 6000 6001 |
| 18 | +
|
| 19 | +Sample Output 1 |
| 20 | +2 2 3 |
| 21 | +3 1 2 3 |
| 22 | +3 1 3 4 |
| 23 | +*/ |
| 24 | + |
| 25 | +import java.util.*; |
| 26 | + |
| 27 | +public class JavelinQualification |
| 28 | +{ |
| 29 | + public static void main (String[] args) throws java.lang.Exception |
| 30 | + { |
| 31 | + // your code goes here |
| 32 | + Scanner sc = new Scanner(System.in); |
| 33 | + int t = sc.nextInt(); |
| 34 | + while(t-->0){ |
| 35 | + int n = sc.nextInt(); |
| 36 | + int m = sc.nextInt(); |
| 37 | + int x = sc.nextInt(); |
| 38 | + int[] arr = new int[n]; // creating array which stores throws of each player |
| 39 | + int[] player = new int[n]; |
| 40 | + int count = 0; |
| 41 | + HashMap<Integer,Integer> map = new HashMap<>(); |
| 42 | + int index = 0; |
| 43 | + for(int i = 0; i < n; i++){ |
| 44 | + arr[i] = sc.nextInt(); |
| 45 | + map.put(arr[i],i+1); |
| 46 | + if(arr[i]>=m){ // if the throw is qualified |
| 47 | + player[index] = i+1; // storing selected players in 'player' array |
| 48 | + arr[i] = 0; |
| 49 | + count++; // storing count of qualified players |
| 50 | + x--; |
| 51 | + index++; |
| 52 | + } |
| 53 | + } |
| 54 | + while(x-->0){ // getting remaining x player who are left |
| 55 | + int max1 = map.getOrDefault(max(arr, n),0); // creating variable to get index of maximum of the remaining players |
| 56 | + if(arr[max1-1]>0){ // checking if the player is not selected |
| 57 | + player[index] = max1; |
| 58 | + arr[max1-1] = 0; |
| 59 | + count++; |
| 60 | + index++; |
| 61 | + } |
| 62 | + } |
| 63 | + Arrays.sort(player); // sorting player in increasing order |
| 64 | + System.out.print(count+" "); |
| 65 | + for(int i = 0; i < n; i++){ |
| 66 | + if(player[i]!=0){ // if the player is qualified |
| 67 | + System.out.print(player[i]+" "); |
| 68 | + } |
| 69 | + } |
| 70 | + System.out.println(); |
| 71 | + } |
| 72 | + sc.close(); |
| 73 | + } |
| 74 | + static int max(int[] arr, int n){ // funtion to get maximum throw |
| 75 | + int max = Integer.MIN_VALUE; |
| 76 | + for(int i = 0; i < n; i++){ |
| 77 | + if(max<arr[i]){ |
| 78 | + max=arr[i]; |
| 79 | + } |
| 80 | + } |
| 81 | + return max; |
| 82 | + } |
| 83 | +} |
0 commit comments