Talentd
FleetCode

3880. Minimum Absolute Difference Between Two Values

Easy66.2% acceptance1 company

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

Asked at 1 company

Hints open one at a time

Hint 1
Use brute force
2:43
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def minAbsoluteDifference(self, nums: list[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,0,0,2,0,1]
Input
[1,0,1,0]
Your code is saved on this device.Solution