Minimum , maximum and average price values for all the items of given type | Java implementation of the approach ; To store an item ; To store the minimum and the maximum price for the item ; To store the total number of items of the current type and the total cost of buying them ; Initializing an element ; Function to find the minimum , the maximum and the average price for every item ; To store the distinct items ; For every item ; If the current item has aready been purchased earlier from a different shop ; Get the item ; Update its minimum and maximum price so far ; Increment the total count of the current item ; Add the current price to the sum ; The item has been purchased for the first time ; Print all the items with their minimum , maximum and average prices ; Driver code
import java . util . * ; class GFG { static class Item { int min , max ; int total , sum ; Item ( int price ) { min = price ; max = price ; total = 1 ; this . sum = price ; } } static void findPrices ( String item [ ] , int price [ ] , int n ) { HashMap < String , Item > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( item [ i ] ) ) { Item currItem = map . get ( item [ i ] ) ; currItem . min = Math . min ( currItem . min , price [ i ] ) ; currItem . max = Math . max ( currItem . max , price [ i ] ) ; currItem . total ++ ; currItem . sum += price [ i ] ; } else { Item currItem = new Item ( price [ i ] ) ; map . put ( item [ i ] , currItem ) ; } } System . out . println ( " Item ▁ Min ▁ Max ▁ Average " ) ; for ( Map . Entry < String , Item > ob : map . entrySet ( ) ) { String key = ob . getKey ( ) ; Item currItem = ob . getValue ( ) ; System . out . println ( key + " ▁ " + currItem . min + " ▁ " + currItem . max + " ▁ " + ( ( float ) currItem . sum / ( float ) currItem . total ) ) ; } } public static void main ( String args [ ] ) { String item [ ] = { " toy " , " pen " , " notebook " , " pen " } ; int n = item . length ; int price [ ] = { 2 , 1 , 3 , 2 } ; findPrices ( item , price , n ) ; } }