fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) throws IOException {
  6. int T = Integer.parseInt(br.readLine().trim());
  7.  
  8. while (T-- > 0) {
  9. int n = Integer.parseInt(br.readLine().trim());
  10. int[] a = new int[n];
  11. int[] b = new int[n];
  12.  
  13. StringTokenizer st = new StringTokenizer(br.readLine());
  14. for (int i = 0; i < n; i++) {
  15. a[i] = Integer.parseInt(st.nextToken());
  16. }
  17. st = new StringTokenizer(br.readLine());
  18. for (int i = 0; i < n; i++) {
  19. b[i] = Integer.parseInt(st.nextToken());
  20. }
  21.  
  22. Arrays.sort(a);
  23. Arrays.sort(b);
  24.  
  25. boolean possible = true;
  26. int i = 0, j = 0;
  27. while (i < n && j < n) {
  28. if (a[i] == b[j]) {
  29. i++;
  30. j++;
  31. } else if (a[i] + 1 == b[j]) {
  32. i++;
  33. j++;
  34. } else {
  35. possible = false;
  36. break;
  37. }
  38. }
  39. if (i != n || j != n) possible = false;
  40.  
  41. System.out.println(possible ? "Yes" : "No");
  42. }
  43. }
  44. }
Success #stdin #stdout 0.09s 53008KB
stdin
2
3
1 2 3
2 2 3
4
1 2 1 2
1 1 4 3
stdout
Yes
No