2903. Find Indices With Index and Value Difference I LeetCode Solution

In this guide, you will get 2903. Find Indices With Index and Value Difference I LeetCode Solution with the best time and space complexity. The solution to Find Indices With Index and Value Difference I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Indices With Index and Value Difference I solution in C++
  4. Find Indices With Index and Value Difference I solution in Java
  5. Find Indices With Index and Value Difference I solution in Python
  6. Additional Resources
2903. Find Indices With Index and Value Difference I LeetCode Solution image

Problem Statement of Find Indices With Index and Value Difference I

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, and
abs(nums[i] – nums[j]) >= valueDifference

Return 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:

See also  1519. Number of Nodes in the Sub-Tree With the Same Label LeetCode Solution

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 <= 100
0 <= nums[i] <= 50
0 <= indexDifference <= 100
0 <= valueDifference <= 50

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2903. Find Indices With Index and Value Difference I LeetCode Solution in C++

class Solution {
 public:
  vector<int> findIndices(vector<int>& nums, int indexDifference,
                          int valueDifference) {
    // nums[minIndex] := the minimum number with enough index different from the
    // current number
    int minIndex = 0;
    // nums[maxIndex] := the maximum number with enough index different from the
    // current number
    int maxIndex = 0;

    for (int i = indexDifference; i < nums.size(); ++i) {
      if (nums[i - indexDifference] < nums[minIndex])
        minIndex = i - indexDifference;
      if (nums[i - indexDifference] > nums[maxIndex])
        maxIndex = i - indexDifference;
      if (nums[i] - nums[minIndex] >= valueDifference)
        return {i, minIndex};
      if (nums[maxIndex] - nums[i] >= valueDifference)
        return {i, maxIndex};
    }

    return {-1, -1};
  }
};
/* code provided by PROGIEZ */

2903. Find Indices With Index and Value Difference I LeetCode Solution in Java

class Solution {
  public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
    // nums[minIndex] := the minimum number with enough index different from the
    // current number
    int minIndex = 0;
    // nums[maxIndex] := the maximum number with enough index different from the
    // current number
    int maxIndex = 0;

    for (int i = indexDifference; i < nums.length; ++i) {
      if (nums[i - indexDifference] < nums[minIndex])
        minIndex = i - indexDifference;
      if (nums[i - indexDifference] > nums[maxIndex])
        maxIndex = i - indexDifference;
      if (nums[i] - nums[minIndex] >= valueDifference)
        return new int[] {i, minIndex};
      if (nums[maxIndex] - nums[i] >= valueDifference)
        return new int[] {i, maxIndex};
    }

    return new int[] {-1, -1};
  }
}
// code provided by PROGIEZ

2903. Find Indices With Index and Value Difference I LeetCode Solution in Python

class Solution:
  def findIndices(
      self,
      nums: list[int],
      indexDifference: int,
      valueDifference: int,
  ) -> list[int]:
    # nums[minIndex] := the minimum number with enough index different from the
    # current number
    minIndex = 0
    # nums[maxIndex] := the maximum number with enough index different from the
    # current number
    maxIndex = 0

    for i in range(indexDifference, len(nums)):
      if nums[i - indexDifference] < nums[minIndex]:
        minIndex = i - indexDifference
      if nums[i - indexDifference] > nums[maxIndex]:
        maxIndex = i - indexDifference
      if nums[i] - nums[minIndex] >= valueDifference:
        return [i, minIndex]
      if nums[maxIndex] - nums[i] >= valueDifference:
        return [i, maxIndex]

    return [-1, -1]
# code by PROGIEZ

Additional Resources

See also  639. Decode Ways II LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.