Advertisement
unknown_0711

Untitled

Feb 24th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. class Solution {
  6.  
  7.  
  8. public int minMax(int[] X, int[] Y, int[] Z) {
  9. int difference = Integer.MAX_VALUE;
  10. int minimum = Integer.MIN_VALUE;
  11. int maximum = Integer.MAX_VALUE;
  12.  
  13.  
  14. int i = 0, j = 0, k = 0;
  15. int n = X.length;
  16. int m = Y.length;
  17. int o = Z.length;
  18. while (i < n && j < m && k < o) {
  19.  
  20.  
  21. minimum = Math.min(X[i], Math.min(Y[j], Z[k]));
  22. maximum = Math.max(X[i], Math.max(Y[j], Z[k]));
  23. difference = Math.min(difference, maximum - minimum);
  24.  
  25.  
  26. // If difference = 0, then we can return the difference as the minimum value.
  27. if (difference == 0) {
  28. break;
  29. }
  30.  
  31.  
  32. // If the element from the array X is minimum.
  33. if (X[i] == minimum) {
  34. i++;
  35. }
  36.  
  37.  
  38. // If the element from the array Y is minimum.
  39. else if (Y[j] == minimum) {
  40. j++;
  41. }
  42.  
  43.  
  44. // If the element from the array Z is minimum.
  45. else {
  46. k++;
  47. }
  48. }
  49.  
  50.  
  51. return difference;
  52. }
  53.  
  54.  
  55. }
  56.  
  57.  
  58. public class Main {
  59. public static void main(String[] args) throws IOException {
  60. Scanner sc = new Scanner(System.in);
  61. int x = sc.nextInt();
  62. int[] X = new int[x];
  63. for (int i = 0; i < x; i++) {
  64. X[i] = sc.nextInt();
  65. }
  66. int y = sc.nextInt();
  67. int[] Y = new int[y];
  68. for (int i = 0; i < y; i++) {
  69. Y[i] = sc.nextInt();
  70. }
  71. int z = sc.nextInt();
  72. int[] Z = new int[z];
  73. for (int i = 0; i < z; i++) {
  74. Z[i] = sc.nextInt();
  75. }
  76. sc.close();
  77. Solution Obj = new Solution();
  78. System.out.print(Obj.minMax(X, Y, Z));
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement