LeetCode 2903: Find Indices With Index and Value Difference I

ArrayTwo Pointers

Problem Description

Explanation:

To solve this problem, we can iterate through all pairs of indices i and j and check if the conditions abs(i - j) >= indexDifference and abs(nums[i] - nums[j]) >= valueDifference are satisfied. If we find such a pair, we return the indices i and j. If no such pair exists, we return [-1, -1].

  • Time Complexity: O(n^2) where n is the length of the input array nums.
  • Space Complexity: O(1)

Solutions

class Solution {
    public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i != j && Math.abs(i - j) >= indexDifference && Math.abs(nums[i] - nums[j]) >= valueDifference) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }
}

Loading editor...