# Find K items with the lowest values

Given a list of items and their values. The task is to find k items with the lowest value. It is possible that two items have the same value, in that case item whose name comes first (lexicographically) will be given higher priority.

Examples:

Input : items[] = {Bat, Gloves, Wickets, Ball}, values[] = {100, 50, 200, 100} k = 2 Output : Gloves Ball Explanation : Gloves has the lowest value. Ball and Bat has the same value but Ball comes first lexicographically.

**Approach:**

This question can be solved by picking the items greedily according to the values. We will sort use the items list in the ascending order of the values and in case of the same values items will be sorted lexicographical order increasing order.

We will store the data in the form of pairs in a vector and will use an inbuilt sort function with boolean compactor function which will be used to compare two items.

Below is the implementation of the above approach:

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Boolean Comparator Function ` `// to compare two pairs of item-value ` `bool` `comp(pair<string, ` `int` `> A, pair<string, ` `int` `> B) ` `{ ` ` ` `// Compare the name if the values are equal ` ` ` `if` `(A.second == B.second) ` ` ` `return` `A.first < B.first; ` ` ` ` ` `// Else compare values ` ` ` `return` `A.second < B.second; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `k = 2; ` ` ` `int` `n = 3; ` ` ` ` ` `// Store data in a vector of Item-Value Pair ` ` ` `vector<pair<string, ` `int` `> > items; ` ` ` ` ` `// inserting items-value pairs in the vector ` ` ` `items.push_back(make_pair(` `"Bat"` `, 100)); ` ` ` `items.push_back(make_pair(` `"Gloves"` `, 50)); ` ` ` `items.push_back(make_pair(` `"Wickets"` `, 200)); ` ` ` `items.push_back(make_pair(` `"Ball"` `, 100)); ` ` ` ` ` `// Sort items using Inbuilt function ` ` ` `sort(items.begin(), items.end(), comp); ` ` ` ` ` `// Print top k values ` ` ` `// or if n is less than k ` ` ` `// Print all n items ` ` ` `for` `(` `int` `i = 0; i < min(n, k); ++i) { ` ` ` `cout << items[i].first << ` `'\n'` `; ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output :**

Gloves Ball

Time Complexity – O(NlogN)

**Further Optimization : ** We can use heap based method to find k largest elements efficiently.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum, maximum and average price values for all the items of given type
- Get maximum items when other items of total cost of an item are free
- Find the top K items with the highest value
- Lowest Common Ancestor in Parent Array Representation
- Find minimum value of y for the given x values in Q queries from all the given set of lines
- Find Partition Line such that sum of values on left and right is equal
- Find the original Array using XOR values of all adjacent elements
- Find duplicates in an Array with values 1 to N using counting sort
- Find minimum number of currency notes and values that sum to given amount
- Mapping external values to dataframe values in Pandas
- Sort first k values in ascending order and remaining n-k values in descending order
- Buy minimum items without change and given coins
- Minimum number of items to be delivered
- Maximum profit by selling N items at two markets
- Randomly select items from a List in Java
- Minimum number of distinct elements after removing m items
- Check if two items can be selected from two different categories without exceeding price
- Count the number of ways to fill K boxes with N distinct items
- Maximum items that can be bought from the cost Array based on given conditions
- Calculate the loss incurred in selling the given items at discounted price

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.