A company has a number of products in stock. They have multiple products available for a single product ID. The product IDs of the products are denoted by 1 to N. The company wants to analyze the difference between the maximum number of products available and the minimum number of products available for any two different product IDs so that they can increase the supply of the products which are low in stock. In addition, the product with a higher number of products in stock should also have a greater product ID than the product ID which has fewer items in stock. Write an algorithm to find the maximum difference between the minimum and the maximum number of products available for a given list of product IDs Input The first line of the input consists of an integer - num, representing the number
Answers
Answer:
Mark me as brainliests and I hope it's help you
Answer:
import java.util.*;
public class HelloWorld{
public static int check(int[] arr,int n){
Map<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
mp.put(arr[i],
mp.getOrDefault(arr[i], 0) + 1);
}
List<Map.Entry<Integer, Integer> > list
= new ArrayList<Map.Entry<Integer,
Integer> >(
mp.entrySet());
Collections.sort(
list,
new Comparator<Map.Entry<Integer,
Integer> >()
{
public int compare(
Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
if (o1.getValue() == o2.getValue())
return o2.getKey() - o1.getKey();
else
return o2.getValue()
- o1.getValue();
}
});
System.out.println(list);
if(list.size()<=1)
return 0;
else{
if(list.get(0).getKey() > list.get(list.size()-1).getKey())
return (list.get(0).getKey())-list.get(list.size()-1).getKey();
else
return 0;
}
}
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++){
int a=sc.nextInt();
arr[i]=a;
}
System.out.println(check(arr,n));
}
}
Explanation: