Problem Description
Given an integer array nums, and two integers indexDifference and valueDifference, find two indices i and j (they can be the same) such that:
- abs(i - j) >= indexDifference
- abs(nums[i] - nums[j]) >= valueDifference Return the pair [i, j] if it exists or [-1, -1] otherwise.
Key Insights
- The constraints allow a brute-force approach since n is small (n <= 100).
- Iterate over all pairs (i, j) and check both conditions.
- Since indices can be the same, the check for indexDifference being zero needs to be correctly handled.
- Multiple valid pairs may exist; return any that satisfies the conditions.
Space and Time Complexity
Time Complexity: O(n^2) because we are iterating over all pairs in the array.
Space Complexity: O(1) as only constant extra space is used.
Solution
We can solve the problem using a two-loop brute-force approach. For every pair of indices (i, j), we check:
- If the absolute difference in indices is greater than or equal to indexDifference.
- If the absolute difference in values from the array at these indices is greater than or equal to valueDifference. If both conditions are satisfied, return the pair [i, j]. If the loops finish without finding such a pair, return [-1, -1].