-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighpeak.java
More file actions
44 lines (39 loc) · 1.61 KB
/
Copy pathHighpeak.java
File metadata and controls
44 lines (39 loc) · 1.61 KB
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
import java.util.Arrays;
import java.util.Scanner;
public class Highpeak {
static int minDiff(int array[], int N, int M)
{
int result = Integer.MAX_VALUE;
Arrays.sort(array);
for (int i=0; i<= N-M;i++)
result = Math.min(result, array[i + M - 1] - array[i]);
return result;
}
static int findelements(int res,int array[], int N, int M)
{
int result = Integer.MAX_VALUE;
for(int i=0; i<=N; i++)
{
result = Math.min(result, array[i + M - 1] - array[i]);
if (res==result)
return i;
}
return 0;
}
public static void main(String[] args) {
int array[]={7980,22349,999,2799,229900,11101,9999,2195,9800,4999};
String items[]={ "MI Band: 999","Sandwich Toaster: 2195" ,"Cult Pass: 2799","Scale: 4999","Fitbit Plus: 7980","Microwave Oven: 9800" ,"Alexa: 9999","Digital Camera: 11101", "IPods: 22349","Macbook Pro: 229900" };
int N = array.length;
System.out.println("Enter the number of employees");
Scanner s = new Scanner(System.in);
int M=s.nextInt();
int result=minDiff(array, N, M);
System.out.println("Number of the employees:"+M);
int startindex=findelements(result,array,N,M);
System.out.println("Here the goodies that are selected for distribution are:");
for(int i=startindex;i<startindex+M;i++)
System.out.println(items[i]);
System.out.println("\n");
System.out.println("And the difference between the chosen goodies with highest price and the lowest price is:"+result);
}
}