Skip to content

https://github.com/Hazi123456 #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Hazi123456 opened this issue Oct 23, 2021 · 0 comments
Open

https://github.com/Hazi123456 #35

Hazi123456 opened this issue Oct 23, 2021 · 0 comments

Comments

@Hazi123456
Copy link

INPUT

Number of employees : 4
Here the input_items that are selected for distribution are:
Fitbit Plus : 7980
Microwave Oven : 9800
Alexa : 9999
Digital Camera : 11101

And the difference between the chosen goodie with highest price and the lowest price is 3121
Number of employees : 2
Here the input_items that are selected for distribution are:
Cult Pass : 2799
Scale : 4999
And the difference between the chosen goodie with highest price and the lowest price is 199
Number of employees : 4
Here the input_items that are selected for distribution are:
Fitbit Plus : 7980
Microwave Oven : 9800
Alexa : 9999
Digital Camera : 11101

And the difference between the chosen goodie with highest price and the lowest price is 3121
Number of employees : 2
Here the input_items that are selected for distribution are:
Cult Pass : 2799
Scale : 4999
And the difference between the chosen goodie with highest price and the lowest price is 199

PROGRAM

package github;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Test1 {

static int minDiff(int arr[], int n, int k) {
	int result = Integer.MAX_VALUE;
	Arrays.sort(arr);
	for (int i=0; i<= n-k;i++)
		result = Math.min(result, arr[i + k - 1] - arr[i]);
	return result;
	
}
static int findele(int res,int arr[], int n, int k)
{
	int result = Integer.MAX_VALUE;
	for(int i=0; i<=n; i++)
	{
		result = Math.min(result, arr[i + k - 1] - arr[i]);
		if (res==result)
			return i;
				
	}
	return 0;
}
public static void main(String[] args) throws IOException  {
    
    int arr[]={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 = arr.length;
    System.out.println("Enter the number of employees");
    Scanner s = new Scanner(System.in);
    int k=s.nextInt();
    int result=minDiff(arr, n, k);  
    System.out.println("Number of the employees:"+k); 
    int startindex=findele(result,arr,n,k);
    System.out.println("Here the goodies that are selected for distribution are:");
    for(int i=startindex;i<startindex+k;i++)
    System.out.println(items[i]);
      System.out.println("And the difference between the chosen goodie with highest price and the lowest price is:"+result);
} 

	
}

OUTPUT

Enter the number of employees
4
Number of the employees:4
Here the goodies that are selected for distribution are:
Fitbit Plus: 7980
Microwave Oven: 9800
Alexa: 9999
Digital Camera: 11101
And the difference between the chosen goodie with highest price and the lowest price is:3121

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant