Track your progress, run tests, and get AI feedback
Run code and see test results
Submit and get AI-powered feedback
Track which problems you've solved
Array Pair Sum: Find Indices for Target Value (Two Sum)
This problem challenges you to find two distinct elements within an array of numbers that sum up to a specific target value. It's a fundamental problem often used to assess basic array manipulation and search techniques. You will return the 0-based indices of these two numbers.
Implement a function findPairSum that takes an array of integers prices and an integer targetBudget. Your task is to:
i and j in the prices array such that prices[i] + prices[j] == targetBudget.[i, j] as an array of integers.prices: An array of integers representing item prices.targetBudget: An integer representing the desired sum.[index1, index2], where index1 and index2 are the 0-based indices of the two numbers that sum to targetBudget. If no such pair exists, return an empty array [].index1 must not be equal to index2.[].Examples
Example 1
Input:
prices = [2, 7, 11, 15], targetBudget = 9
Output:
[0, 1]
Note:
Because prices[0] + prices[1] = 2 + 7 = 9.
Example 2
Input:
prices = [3, 2, 4], targetBudget = 6
Output:
[1, 2]
Note:
Because prices[1] + prices[2] = 2 + 4 = 6.
Example 3
Input:
prices = [3, 3], targetBudget = 6
Output:
[0, 1]
Note:
Because prices[0] + prices[1] = 3 + 3 = 6. Note that different indices are used even for identical values.
Example 4
Input:
prices = [1, 5, 9], targetBudget = 100
Output:
[]
Note:
No two numbers in the array sum up to 100.
Constraints
2 <= prices.length <= 10^4-10^9 <= prices[i] <= 10^9-10^9 <= targetBudget <= 10^9prices[i] is an integer.