Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class ArrayStabilization {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- ArrayList<Integer> lists = new ArrayList<>();
- int inputs = sc.nextInt();
- for (int i = 0; i < inputs; i++) {
- lists.add(sc.nextInt());
- }
- HashSet<Integer> sets = new HashSet<>();
- ArrayList<Integer> a = new ArrayList<>(lists);
- IntSummaryStatistics intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
- int max = intStream.getMax();
- a.remove(new Integer(max));
- intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
- int max1 = intStream.getMax();
- int min1 = intStream.getMin();
- sets.add(max1 - min1);
- a = new ArrayList<>(lists);
- intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
- int minimum = intStream.getMin();
- a.remove(new Integer(minimum));
- intStream = a.stream().collect(Collectors.summarizingInt(Integer::intValue));
- max1 = intStream.getMax();
- min1 = intStream.getMin();
- sets.add(max1 - min1);
- Optional<Integer> min = sets.stream().min(Integer::compareTo);
- if (min.isPresent()) {
- System.out.println(min.get());
- } else {
- System.out.println(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement