|
| 1 | +package hackerRank_JavaProblemSolving; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Scanner; |
| 8 | + |
| 9 | +public class Problem083_PermutingTwoArrays__A_Possibility { |
| 10 | + |
| 11 | + // Algorithmic complexity O(2nlogn) |
| 12 | + // THE IDEA: it asked about possibility of existence such permutations |
| 13 | + static String twoArrays(int k, int[] A, int[] B, int n) { |
| 14 | + Arrays.sort(A); |
| 15 | + Arrays.sort(B); |
| 16 | + for (int i = 0; i < n; i++) { |
| 17 | + if (A[i]+B[n-i-1] < k) { |
| 18 | + return "NO"; |
| 19 | + } |
| 20 | + } |
| 21 | + return "YES"; |
| 22 | + } |
| 23 | + |
| 24 | + private static final Scanner scanner = new Scanner(System.in); |
| 25 | + |
| 26 | + public static void main(String[] args) throws IOException { |
| 27 | + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); |
| 28 | + |
| 29 | + int q = scanner.nextInt(); |
| 30 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 31 | + |
| 32 | + for (int qItr = 0; qItr < q; qItr++) { |
| 33 | + String[] nk = scanner.nextLine().split(" "); |
| 34 | + |
| 35 | + int n = Integer.parseInt(nk[0]); |
| 36 | + |
| 37 | + int k = Integer.parseInt(nk[1]); |
| 38 | + |
| 39 | + int[] A = new int[n]; |
| 40 | + |
| 41 | + String[] AItems = scanner.nextLine().split(" "); |
| 42 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 43 | + |
| 44 | + for (int i = 0; i < n; i++) { |
| 45 | + int AItem = Integer.parseInt(AItems[i]); |
| 46 | + A[i] = AItem; |
| 47 | + } |
| 48 | + |
| 49 | + int[] B = new int[n]; |
| 50 | + |
| 51 | + String[] BItems = scanner.nextLine().split(" "); |
| 52 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 53 | + |
| 54 | + for (int i = 0; i < n; i++) { |
| 55 | + int BItem = Integer.parseInt(BItems[i]); |
| 56 | + B[i] = BItem; |
| 57 | + } |
| 58 | + |
| 59 | + String result = twoArrays(k, A, B, n); |
| 60 | + |
| 61 | + bufferedWriter.write(result); |
| 62 | + bufferedWriter.newLine(); |
| 63 | + } |
| 64 | + |
| 65 | + bufferedWriter.close(); |
| 66 | + |
| 67 | + scanner.close(); |
| 68 | + } |
| 69 | +} |
0 commit comments