SHARE
TWEET

Untitled

shniaga Feb 26th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ListsMoreExercise;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class MixedUpLists {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> list1 = Arrays.
  11.                 stream(scanner.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  12.         List<Integer> list2 = Arrays.
  13.                 stream(scanner.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  14.  
  15.         List<Integer> list3 = new ArrayList<>();
  16.         List<Integer> finalList = new ArrayList<>();
  17.         int firstConstrain = 0;
  18.         int secondConstrain = 0;
  19.  
  20.         if (list1.size() > list2.size()) {
  21.             firstConstrain = list1.get(list1.size() - 2);
  22.             secondConstrain = list1.get(list1.size() - 1);
  23.             Collections.reverse(list2);
  24.             for (int i = 0; i < list2.size(); i++) {
  25.                 list3.add(list1.get(i));
  26.                 list3.add(list2.get(i));
  27.             }
  28.         } else if (list1.size() < list2.size()) {
  29.             firstConstrain = list2.get(list2.size() - 2);
  30.             secondConstrain = list2.get(list2.size() - 1);
  31.             Collections.reverse(list2);
  32.             for (int i = 0; i < list1.size(); i++) {
  33.                 list3.add(list1.get(i));
  34.                 list3.add(list2.get(i+2));
  35.             }
  36.         }
  37.         int smallerNumber = Math.min(firstConstrain, secondConstrain);
  38.         int biggerNumber = Math.max(firstConstrain, secondConstrain);
  39.  
  40.         for (int i = 0; i < list3.size(); i++) {
  41.             if (list3.get(i) > smallerNumber && list3.get(i) < biggerNumber) {
  42.                 finalList.add(list3.get(i));
  43.             }
  44.         }
  45.         Collections.sort(finalList);
  46.  
  47.         finalList.forEach(e -> System.out.print(e + " "));
  48.  
  49.  
  50.  
  51.  
  52.     }
  53. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top