-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChocolateDistribution.java
47 lines (39 loc) · 1.04 KB
/
ChocolateDistribution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*Test Case 1:
Input : n = 5, a[] = {3, 2, 7, 6, 1} , m = 3
Output: Minimum difference is 2
Test Case 2:
Input : n = 7, a[] = {8, 5, 6, 3, 2, 7, 1} , m = 5
Output: Minimum difference is 5
*/
import java.util.*;
class ChocolateDistribution {
static int Min( int n,int a[],int m)
{
if (m == 0 || n == 0)
return 0;
Arrays.sort(a);
if (n < m)
return -1;
int min = Integer.MAX_VALUE;
for (int i = 0; i + m - 1 < n; i++)
{
int diff = a[i+m-1] - a[i];
if (diff < min)
min = diff;
}
return min;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
int m = sc.nextInt();
System.out.println("Minimum difference is "
+ Min(n,a,m));
}
}