Target Bundle Price Check
Razorpay helps millions of businesses accept, process, and disburse payments. One common business operation is creating product bundles or promotions. Merchants often need to determine if a specific combination of products can achieve an exact target price for a promotional bundle. This problem focuses on efficiently solving this calculation.
Implement a function canAchieveTargetPrice that determines if a given targetPrice can be exactly matched by summing a subset of product prices from a provided list. Each product price can be used at most once.
Input:
productPrices: An array of integers representing the individual prices of available products. Each price is a positive integer.targetPrice: An integer representing the exact price that needs to be achieved for the bundle. targetPrice is a non-negative integer.Output:
true if a subset of productPrices sums up exactly to targetPrice, false otherwise.productPrices list should only return true if targetPrice is 0. Otherwise, it's false.Examples
Example 1
Input:
productPrices = [2, 3, 7, 8], targetPrice = 10
Output:
true
Note:
A bundle with prices [2, 8] sums up to 10.
Example 2
Input:
productPrices = [5, 10, 15], targetPrice = 12
Output:
false
Note:
No combination of products from [5, 10, 15] sums up to exactly 12.
Example 3
Input:
productPrices = [1], targetPrice = 0
Output:
true
Note:
An empty bundle (no products chosen) sums to 0. This is always achievable.
Example 4
Input:
productPrices = [], targetPrice = 5
Output:
false
Note:
No products are available to form a bundle, so 5 cannot be achieved.
Constraints
1 <= productPrices.length <= 1001 <= productPrices[i] <= 10000 <= targetPrice <= 1000100,000.Loading editor...