You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
abs(i - j) >= indexDifference, andabs(nums[i] - nums[j]) >= valueDifferenceReturn an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4 Output: [0,3] Explanation: In this example, i = 0 and j = 3 can be selected. abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4. Hence, a valid answer is [0,3]. [3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0 Output: [0,0] Explanation: In this example, i = 0 and j = 0 can be selected. abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0. Hence, a valid answer is [0,0]. Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4 Output: [-1,-1] Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions. Hence, [-1,-1] is returned.
Constraints:
1 <= n == nums.length <= 1000 <= nums[i] <= 500 <= indexDifference <= 1000 <= valueDifference <= 50When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a row of items, and you need to find a pair of items that are far enough apart from each other and also have a big difference in their values. The brute force approach simply compares every single possible pair of items one by one.
Here's how the algorithm would work step-by-step:
def find_indices(nums, index_difference, value_difference):
# Compare every possible pair to check both index separation and value magnitude
for outer_index in range(len(nums)):
for inner_index in range(outer_index + 1, len(nums)):
# Ensure the distance between indices meets the minimum requirement
if abs(outer_index - inner_index) >= index_difference:
# Ensure the absolute value difference meets the minimum requirement
if abs(nums[outer_index] - nums[inner_index]) >= value_difference:
return [outer_index, inner_index]
# Return negative indices if no valid pair satisfies all constraints
return [-1, -1]The algorithm finds two indices meeting both the minimum index gap and minimum value gap constraints. It slides an index across the array while maintaining the locations of the minimum and maximum values encountered in the valid prefix window. Comparing each element against only the running minimum and maximum guarantees optimal checks in linear time.
Here's how the algorithm would work step-by-step:
def find_indices(nums: list[int], indexDifference: int, valueDifference: int) -> list[int]:
# Track the running indices of minimum and maximum elements seen so far
min_idx = 0
max_idx = 0
# Iterate through each valid right index that maintains the required distance
for right_idx in range(indexDifference, len(nums)):
left_idx = right_idx - indexDifference
if nums[left_idx] < nums[min_idx]:
min_idx = left_idx
if nums[left_idx] > nums[max_idx]:
max_idx = left_idx
# Check if difference with the running minimum satisfies the requirement
if nums[right_idx] - nums[min_idx] >= valueDifference:
return [min_idx, right_idx]
# Check if difference with the running maximum satisfies the requirement
if nums[max_idx] - nums[right_idx] >= valueDifference:
return [max_idx, right_idx]
return [-1, -1]
| Case | How to Handle |
|---|---|
| Array length smaller than indexDifference plus one | Return the empty result immediately as no pair can satisfy the minimum index distance requirement. |
| Negative numbers and mixed-sign elements in the array | Use absolute difference functions for value comparisons to ensure correct handling of negative values. |
| Array containing all identical values | The solution will correctly return empty if the value difference requirement cannot be met by any pair. |
| No valid pair exists meeting both conditions | The algorithm finishes the loops and returns the designated empty result pair. |
| Multiple valid pairs exist in the array | The solution returns the first valid pair found based on the iteration order. |
| Maximum value extremes causing integer overflow | Use language-appropriate wider integer types or safe subtraction methods to prevent overflow during absolute difference calculations. |
| Large inputs approaching the upper constraint limits | Optimize the search using a tracking approach for min and max values to avoid an inefficient brute-force nested loop. |
| Index difference set to zero | The condition allows any two distinct indices since the distance threshold is zero. |