|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class kthInt{ |
| 4 | + public static void main (String[] args){ |
| 5 | + Scanner sc = new Scanner(System.in); |
| 6 | + |
| 7 | + System.out.println("Type the numbers of elements you want to store: "); |
| 8 | + int num = sc.nextInt(); |
| 9 | + int[] nums = new int[num]; |
| 10 | + |
| 11 | + System.out.println("Type the elements: "); |
| 12 | + for(int i = 0; i<num; i++){ |
| 13 | + nums[i] = sc.nextInt(); |
| 14 | + } |
| 15 | + System.out.println("Type number of cases you want to test: "); |
| 16 | + int cases = sc.nextInt(); |
| 17 | + int[][] test = new int[cases][3]; |
| 18 | + |
| 19 | + System.out.println("Type the elements for each cases: "); |
| 20 | + for(int i = 0; i<cases; i++){ |
| 21 | + for ( int j = 0; j < 3; j++){ |
| 22 | + test[i][j] = sc.nextInt(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + sc.close(); |
| 27 | + int [] finalanswer = solution(nums, test); |
| 28 | + System.out.println(); |
| 29 | + System.out.print('['); |
| 30 | + for(int i: finalanswer){ |
| 31 | + |
| 32 | + if(i == finalanswer[cases-1]){ |
| 33 | + System.out.print(i); |
| 34 | + }else{ |
| 35 | + System.out.print(i + ","); |
| 36 | + } |
| 37 | + } |
| 38 | + System.out.print(']'); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + public static int[] solution (int [] array, int[][] commands){ |
| 43 | + int row = commands.length; |
| 44 | + int[] answer = new int [row]; |
| 45 | + for (int i = 0; i < row; i++){ |
| 46 | + int[] newarr = Arrays.copyOfRange(array, (commands[i][0] - 1) , (commands[i][1])); |
| 47 | + Arrays.sort(newarr); |
| 48 | + answer[i] = newarr[commands[i][2] - 1]; |
| 49 | + } |
| 50 | + |
| 51 | + return answer; |
| 52 | + } |
| 53 | +} |
0 commit comments