Advertisement
T-D-K

Untitled

Dec 27th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.46 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class ArrayStabilization {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         ArrayList<Integer> lists = new ArrayList<>();
  10.         int inputs = sc.nextInt();
  11.         for (int i = 0; i < inputs; i++) {
  12.             lists.add(sc.nextInt());
  13.         }
  14.  
  15.         HashSet<Integer> sets = new HashSet<>();
  16.         ArrayList<Integer> a = new ArrayList<>(lists);
  17.         IntSummaryStatistics intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
  18.         int max = intStream.getMax();
  19.         a.remove(new Integer(max));
  20.         intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
  21.         int max1 = intStream.getMax();
  22.         int min1 = intStream.getMin();
  23.         sets.add(max1 - min1);
  24.  
  25.  
  26.         a = new ArrayList<>(lists);
  27.         intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
  28.         int minimum = intStream.getMin();
  29.         a.remove(new Integer(minimum));
  30.         intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
  31.         max1 = intStream.getMax();
  32.         min1 = intStream.getMin();
  33.         sets.add(max1 - min1);
  34.  
  35.         Optional<Integer> min = sets.stream().min(Integer::compareTo);
  36.         if (min.isPresent()) {
  37.             System.out.println(min.get());
  38.         } else {
  39.             System.out.println(0);
  40.         }
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement