Skip to content

3880. Minimum Absolute Difference Between Two Values

Description

You are given an integer array nums consisting only of 0, 1, and 2.

A pair of indices (i, j) is called valid if nums[i] == 1 and nums[j] == 2.

Return the minimum absolute difference between i and j among all valid pairs. If no valid pair exists, return -1.

The absolute difference between indices i and j is defined as abs(i - j).

Β 

Example 1:

Input: nums = [1,0,0,2,0,1]

Output: 2

Explanation:

The valid pairs are:

  • (0, 3) which has absolute difference of abs(0 - 3) = 3.
  • (5, 3) which has absolute difference of abs(5 - 3) = 2.

Thus, the answer is 2.

Example 2:

Input: nums = [1,0,1,0]

Output: -1

Explanation:

There are no valid pairs in the array, thus the answer is -1.

Β 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 2

Solutions

Solution 1: Single Pass

We use an array \(\textit{last}\) of length \(3\) to record the last occurrence index of digits \(0\), \(1\), and \(2\). Initially, \(\textit{last} = [-(n+1), -(n+1), -(n+1)]\). We iterate through the array \(\textit{nums}\). For the current number \(x\), if \(x\) is not equal to \(0\), we update the answer \(\textit{ans} = \min(\textit{ans}, i - \textit{last}[3 - x])\), where \(i\) is the index of the current number \(x\). Then we update \(\textit{last}[x] = i\).

After the iteration, if \(\textit{ans}\) is greater than the length of the array \(\textit{nums}\), it means no valid index pair exists, so we return -1; otherwise, we return \(\textit{ans}\).

The time complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{nums}\). The space complexity is \(O(1)\).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def minAbsoluteDifference(self, nums: list[int]) -> int:
        n = len(nums)
        ans = n + 1
        last = [-inf] * 3
        for i, x in enumerate(nums):
            if x:
                ans = min(ans, i - last[3 - x])
                last[x] = i
        return -1 if ans > n else ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
    public int minAbsoluteDifference(int[] nums) {
        int n = nums.length;
        int ans = n + 1;
        int[] last = new int[3];
        Arrays.fill(last, -(n + 1));

        for (int i = 0; i < n; ++i) {
            int x = nums[i];
            if (x != 0) {
                ans = Math.min(ans, i - last[3 - x]);
                last[x] = i;
            }
        }
        return ans > n ? -1 : ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    int minAbsoluteDifference(vector<int>& nums) {
        int n = nums.size();
        int ans = n + 1;
        vector<int> last(3, -(n + 1));

        for (int i = 0; i < n; ++i) {
            int x = nums[i];
            if (x != 0) {
                ans = min(ans, i - last[3 - x]);
                last[x] = i;
            }
        }
        return ans > n ? -1 : ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
func minAbsoluteDifference(nums []int) int {
    n := len(nums)
    ans := n + 1

    last := []int{-ans, -ans, -ans}

    for i, x := range nums {
        if x != 0 {
            ans = min(ans, i-last[3-x])
            last[x] = i
        }
    }

    if ans > n {
        return -1
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
function minAbsoluteDifference(nums: number[]): number {
    const n = nums.length;
    let ans = n + 1;
    const last = Array(3).fill(-ans);

    for (let i = 0; i < n; ++i) {
        const x = nums[i];
        if (x) {
            ans = Math.min(ans, i - last[3 - x]);
            last[x] = i;
        }
    }

    return ans > n ? -1 : ans;
}

Comments