Advertisement
Guest User

Untitled

a guest
Sep 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.*;
  2. public class MyClass {
  3.    
  4.     public static int end(int[] in) {
  5.         return in[1];
  6.     }
  7.    
  8.     public static int start(int[] in) {
  9.         return in[0];
  10.     }
  11.    
  12.     public static void main(String args[]) {
  13.         List<int[]> A = Arrays.asList(new int[]{0, 2}, new int[]{5, 10}, new int[]{16, 20});
  14.         List<int[]> B = Arrays.asList(new int[]{1, 5}, new int[]{10, 18}, new int[]{20, 23});
  15.        
  16.         List<int[]> res = new ArrayList<int[]>();
  17.        
  18.         int a=0, b=0;
  19.        
  20.         while( a < A.size() && b < B.size() && ( end(A.get(a)) <= end(B.get(b)) || end(B.get(b)) <= end(A.get(a)))) {
  21.             int x = Math.max(start(A.get(a)), start(B.get(b)));
  22.             int y = Math.min(end(A.get(a)), end(B.get(b)));
  23.            
  24.             int[] temp = new int[]{0, 0};
  25.             temp[0] = x;
  26.             temp[1] = y;
  27.            
  28.             res.add(temp);
  29.            
  30.             if( end(A.get(a)) < end(B.get(b))) {
  31.                 a++;
  32.             } else {
  33.                 b++;
  34.             }
  35.         }
  36.        
  37.         for(int[] i : res) {
  38.             System.out.print(i[0] + ",");
  39.             System.out.print(i[1] + ",");
  40.             System.out.println(" ");
  41.         }
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement