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

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 Average
pen 1 2 1.5
toy 2 2 2.0
notebook 3 3 3.0



Input: item[] = {“car”, “car”}, price[] = {20000, 30000};
Output:
Item Min Max Average
car 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.

  1. min: To store the minimum value for an item of current type.
  2. max: To store the maximum value for an item of current type.
  3. total: Total item of the current type.
  4. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import 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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Item Min Max Average
pen 1 2 1.5
toy 2 2 2.0
notebook 3 3 3.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.