From 6ea213135a75ca0dbdb891204f41d1a7054705c7 Mon Sep 17 00:00:00 2001 From: Jetty53 <73014152+Jetty53@users.noreply.github.com> Date: Sat, 17 Oct 2020 21:09:11 +0530 Subject: [PATCH] Chocolate Distribution Problem --- Array/chocolateDistribution.java | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Array/chocolateDistribution.java diff --git a/Array/chocolateDistribution.java b/Array/chocolateDistribution.java new file mode 100644 index 0000000..2acd27d --- /dev/null +++ b/Array/chocolateDistribution.java @@ -0,0 +1,45 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +/** + * + * This is from a array based problem in GeeksForGeeks. + * + * Given an array A of positive integers of size N, where each value represents number of chocolates in a packet. + * Each packet can have variable number of chocolates. There are M students, the task is to distribute chocolate packets such that : + * 1. Each student gets one packet. + * 2. The difference between the number of chocolates given to the students having packet with maximum chocolates and student having packet with minimum chocolates is minimum. + * + * @author Jetty53 + */ + +class chocolateDistribution { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[] arr = new int[n]; + for (int i = 0; i < n; i++) { + arr[i] = sc.nextInt(); + } + Arrays.sort(arr); + int m = sc.nextInt(); + int res = minDiff(arr, m); + System.out.println(res); + + } + + static int minDiff(int[] arr, int m) { + int min = arr[m - 1] - arr[0]; + int i = 1, j = m; + while (j <= arr.length - 1) { + + if ((arr[j] - arr[i]) < min) { + min = arr[j] - arr[i]; + } + i++; + j++; + } + return min; + } +}