Advertisement
didatzi

P05_Chocolates

Oct 5th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package Excercise.DataRepresentationAndManipulation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class P05_Chocolates {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int barSize = Integer.parseInt(scanner.nextLine());
  13.  
  14.         List<Integer> list = new ArrayList<>();
  15.         String[] input = scanner.nextLine().split(", ");
  16.  
  17.         for (String anInput : input) {
  18.             list.add(Integer.parseInt(anInput));
  19.         }
  20.  
  21.         int minStudents = Integer.parseInt(scanner.nextLine());
  22.         Collections.sort(list);
  23.  
  24.         int min = 0;
  25.         int max = 0;
  26.  
  27.         int minDifference = Integer.MAX_VALUE;
  28.  
  29.         for (int i = 0; i < barSize; i++) {
  30.  
  31.             if (i + minStudents - 1 < list.size()) {
  32.                 min = list.get(i);
  33.                 max = list.get(i + minStudents - 1);
  34.  
  35.                 if (max - min < minDifference) {
  36.                     minDifference = max - min;
  37.                 }
  38.             }
  39.         }
  40.         System.out.printf("Min Difference is %s.", minDifference);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement