Minimum, maximum and average price values for all the items of given type - GeeksforGeeks

28 Oct, 2019
Given a string array item[] and an integer array price[] where price[i] is the price of the item[i] when purchased from the ith shop. The task is find the lowest, the highest and the average price value for all the purchsed items.
Examples:
Input: item[] = {“toy”, “pen”, “notebook”, “pen”}, price[] = {2, 1, 3, 2}Output:Item Min Max Averagepen 1 2 1.5toy 2 2 2.0notebook 3 3 3.0
Input: item[] = {“car”, “car”}, price[] = {20000, 30000};Output:Item Min Max Averagecar 20000 30000 25000.0
Approach: Create a HashMap where the name of the item will be a string and the value will be an object which will store four types of values for every item i.e.
min: To store the minimum value for an item of current type.max: To store the maximum value for an item of current type.total: Total item of the current type.sum: Sum of the prices of the items of current type.
min: To store the minimum value for an item of current type.
max: To store the maximum value for an item of current type.
total: Total item of the current type.
sum: Sum of the prices of the items of current type.
Now, for every item stored in the Hashmap, the minimum and the maximum price is store in the value object and the average can be calculated as sum / total.
Below is the implementation of the above approach:
Java
C#
// Java implementation of the approachimport java.util.*;class GFG {      // To store an item    static class Item {          // To store the minimum and the        // maximum price for the item        int min, max;          // To store the total number of items        // of the current type and the        // total cost of buying them        int total, sum;          // Initializing an element        Item(int price)        {            min = price;            max = price;            total = 1;            this.sum = price;        }    }      // Function to find the minimum, the maximum    // and the average price for every item    static void findPrices(String item[], int price[], int n)    {          // To store the distinct items        HashMap<String, Item> map = new HashMap<>();          // For every item        for (int i = 0; i < n; i++) {              // If the current item has aready been            // purchased earlier from a different shop            if (map.containsKey(item[i])) {                  // Get the item                Item currItem = map.get(item[i]);                  // Update its minimum and maximum price so far                currItem.min = Math.min(currItem.min, price[i]);                currItem.max = Math.max(currItem.max, price[i]);                  // Increment the total count of the current item                currItem.total++;                  // Add the current price to the sum                currItem.sum += price[i];            }            else {                  // The item has been purchased for the first time                Item currItem = new Item(price[i]);                map.put(item[i], currItem);            }        }          // Print all the items with their        // minimum, maximum and average prices        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));        }    }      // Driver code    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);    }}
// C# implementation of the approach using System;         using System.Collections.Generic;  class GFG {      // To store an item    public class Item    {          // To store the minimum and the        // maximum price for the item        public int min, max;          // To store the total number of items        // of the current type and the        // total cost of buying them        public int total, sum;          // Initializing an element        public Item(int price)        {            min = price;            max = price;            total = 1;            this.sum = price;        }    }      // Function to find the minimum, the maximum    // and the average price for every item    static void findPrices(String []item,                               int []price, int n)    {          // To store the distinct items        Dictionary<String,                   Item> map = new Dictionary<String,                                              Item>();          // For every item        for (int i = 0; i < n; i++)        {              // If the current item has aready been            // purchased earlier from a different shop            if (map.ContainsKey(item[i]))            {                  // Get the item                Item currItem = map[item[i]];                  // Update its minimum and                // maximum price so far                currItem.min = Math.Min(currItem.min,                                           price[i]);                currItem.max = Math.Max(currItem.max,                                            price[i]);                  // Increment the total count of                 // the current item                currItem.total++;                  // Add the current price to the sum                currItem.sum += price[i];            }            else             {                  // The item has been purchased                // for the first time                Item currItem = new Item(price[i]);                map.Add(item[i], currItem);            }        }          // Print all the items with their        // minimum, maximum and average prices        Console.WriteLine("Item Min Max Average");        foreach(KeyValuePair<String, Item> ob in map)        {            String key = ob.Key;            Item currItem = ob.Value;            Console.WriteLine(key + " " + currItem.min +                                     " " + currItem.max +                                     " " + ((float)currItem.sum /                                           (float)currItem.total));        }    }      // Driver code    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);    }}  // This code is contributed by 29AjayKumar
Item Min Max Average
pen 1 2 1.5
toy 2 2 2.0
notebook 3 3 3.0

29AjayKumar
Java-HashMap
math
Arrays
Strings
Arrays
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Stack Data Structure (Introduction and Program)
Introduction to Arrays
Multidimensional Arrays in Java
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack