Find Product Insertion Point
At Flipkart, maintaining a vast product catalog requires efficient data management. When new products are added or existing ones are re-priced, it's crucial to quickly determine their correct position to maintain sorted order. This allows for faster searching, indexing, and overall catalog management. This problem focuses on efficiently locating where a new product's price would fit into an already sorted list of product prices.
findInsertionPoint that takes a sorted array of product prices (prices) and a targetPrice as input.targetPrice should be inserted to maintain the ascending sorted order of the prices array.targetPrice is already present, return the index of its first occurrence.targetPrice is greater than all existing prices, return prices.length.prices: An array of numbers (number[]), representing product prices, sorted in non-decreasing order.targetPrice: A number (number), representing the price of the new product.number), representing the index where targetPrice should be inserted.prices array should result in an insertion point of 0.prices array can contain duplicate values.prices and targetPrice) are non-negative integers.Examples
Example 1
Input:
prices = [10, 20, 30, 40], targetPrice = 25
Output:
2
Note:
25 should be inserted between 20 (index 1) and 30 (index 2).
Example 2
Input:
prices = [10, 20, 30, 40], targetPrice = 10
Output:
0
Note:
10 is already present at index 0. We return the index of its first occurrence.
Example 3
Input:
prices = [10, 20, 20, 30, 40], targetPrice = 20
Output:
1
Note:
20 is present multiple times. The first occurrence is at index 1.
Example 4
Input:
prices = [10, 20, 30], targetPrice = 50
Output:
3
Note:
50 is greater than all existing prices, so it should be inserted at the end (index 3).
Example 5
Input:
prices = [], targetPrice = 100
Output:
0
Note:
An empty array, 100 should be inserted at index 0.
Constraints
0 <= prices.length <= 10^50 <= prices[i] <= 10^90 <= targetPrice <= 10^9prices array is always sorted in non-decreasing order.prices array.Loading editor...