Find Products in Price Range
At Bigbasket, customers rely on efficient filtering options to navigate our vast product catalog. A common requirement is to quickly find products within a specific price range. Implementing an optimized solution for this not only enhances user experience by providing responsive search results but also reduces server load by processing data effectively.
Implement a function findProductsInPriceRange that takes an array of product prices (integers), a minimum price, and a maximum price. The function should return a new array containing all product prices that are greater than or equal to the minimum price and less than or equal to the maximum price. The returned array must be sorted in ascending order.
prices: An array of integers representing the prices of various products available in Bigbasket's catalog.minPrice: An integer representing the inclusive lower bound of the desired price range.maxPrice: An integer representing the inclusive upper bound of the desired price range.prices that fall within the [minPrice, maxPrice] range.[].prices[i], minPrice, and maxPrice will be non-negative integers.minPrice > maxPrice? Assume valid range inputs where minPrice <= maxPrice.Examples
Example 1
Input:
prices = [10, 2, 50, 20, 5, 30], minPrice = 10, maxPrice = 30
Output:
[10,20,30]
Note:
After filtering, prices 10, 20, 30 are within the range [10, 30]. Sorted, they are [10, 20, 30].
Example 2
Input:
prices = [1, 5, 10, 15, 20], minPrice = 7, maxPrice = 12
Output:
[10]
Note:
Only price 10 is within the range [7, 12].
Example 3
Input:
prices = [100, 200, 300], minPrice = 10, maxPrice = 50
Output:
[]
Note:
No prices fall within the range [10, 50].
Constraints
1 <= prices.length <= 10^50 <= prices[i] <= 10^90 <= minPrice <= maxPrice <= 10^9prices array is not guaranteed to be sorted.Loading editor...