fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int solution(int[] A, int[] B) {
  11. int n = A.length;
  12. int m = B.length;
  13.  
  14. Arrays.sort(A);
  15. Arrays.sort(B);
  16.  
  17. int i = 0;
  18. int j = 0;
  19. while (i < n && j < m) {
  20. if (A[i] == B[j])
  21. return A[i];
  22. else {
  23. if (A[i] > B[j])
  24. j++;
  25. else
  26. i++;
  27. }
  28. }
  29. return -1;
  30. }
  31. public static void main (String[] args) throws java.lang.Exception
  32. {
  33. int[] x1 = {0, 2, 7, 10, 23, 345, 56, 456, 767, 56, 6, 80, 6, 65};
  34. int[] y1= {1, 4, 5, 9, 9, 9, 80, 67, 77};
  35. System.out.print(solution(x1,y1));
  36. }
  37. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
80