Problem: Minimum Absolute Difference Queries

The minimum absolute difference of an array `a` is defined as the minimum
value of `|a[i] - a[j]|`, where `0 <= i < j < a.length` and `a[i] != a[j]`. If
all elements of `a` are the same, the minimum absolute difference is `-1`.

* For example, the minimum absolute difference of the array `[5,2,3,7,2]` is `|2 - 3| = 1`. Note that it is not `0` because `a[i]` and `a[j]` must be different.

You are given an integer array `nums` and the array `queries` where
`queries[i] = [li, ri]`. For each query `i`, compute the minimum absolute
difference of the subarray `nums[li...ri]` containing the elements of `nums`
between the 0-based indices `li` and `ri` (inclusive).

Return an array `ans` where `ans[i]` is the answer to the `ith` query.

A subarray is a contiguous sequence of elements in an array.

The value of `|x|` is defined as:

* `x` if `x >= 0`.
* `-x` if `x < 0`.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from bisect import bisect_left
from typing import List, Dict, Tuple, Iterator

class Solution:
  def minDifference(self, nums: List[int], queries: List[List[int]]) -> List[int]:
    numToIndices = [[] for _ in range(101)]

    for i, num in enumerate(nums):
      numToIndices[num].append(i)

    if len(numToIndices[nums[0]]) == len(nums):
      return [-1] * len(queries)

    ans = []

    for l, r in queries:
      prevNum = -1
      minDiff = 101
      for num in range(1, 101):
        indices = numToIndices[num]
        i = bisect_left(indices, l)
        if i == len(indices) or indices[i] > r:
          continue
        if prevNum != -1:
          minDiff = min(minDiff, num - prevNum)
        prevNum = num
      ans.append(-1 if minDiff == 101 else minDiff)

    return ans


Java solution:
class Solution {
  public int[] minDifference(int[] nums, int[][] queries) {
    int[] ans = new int[queries.length];
    List<Integer>[] numToIndices = new List[101];

    for (int i = 1; i <= 100; ++i)
      numToIndices[i] = new ArrayList<>();

    for (int i = 0; i < nums.length; ++i)
      numToIndices[nums[i]].add(i);

    if (numToIndices[nums[0]].size() == nums.length) {
      Arrays.fill(ans, -1);
      return ans;
    }

    for (int i = 0; i < queries.length; ++i) {
      final int l = queries[i][0];
      final int r = queries[i][1];
      int prevNum = -1;
      int minDiff = 101;
      for (int num = 1; num <= 100; ++num) {
        List<Integer> indices = numToIndices[num];
        final int j = firstGreaterEqual(indices, l);
        if (j == indices.size() || indices.get(j) > r)
          continue;
        if (prevNum != -1)
          minDiff = Math.min(minDiff, num - prevNum);
        prevNum = num;
      }
      ans[i] = minDiff == 101 ? -1 : minDiff;
    }

    return ans;
  }

  private int firstGreaterEqual(List<Integer> A, int target) {
    final int i = Collections.binarySearch(A, target);
    return i < 0 ? -i - 1 : i;
  }
}


C++ solution:
class Solution {
 public:
  vector<int> minDifference(vector<int>& nums, vector<vector<int>>& queries) {
    vector<vector<int>> numToIndices(101);

    for (int i = 0; i < nums.size(); ++i)
      numToIndices[nums[i]].push_back(i);

    if (numToIndices[nums[0]].size() == nums.size())
      return vector<int>(queries.size(), -1);

    vector<int> ans;

    for (const vector<int>& query : queries) {
      const int l = query[0];
      const int r = query[1];
      int prevNum = -1;
      int minDiff = 101;
      for (int num = 1; num <= 100; ++num) {
        const auto& indices = numToIndices[num];
        const auto it = ranges::lower_bound(indices, l);
        if (it == indices.cend() || *it > r)
          continue;
        if (prevNum != -1)
          minDiff = min(minDiff, num - prevNum);
        prevNum = num;
      }
      ans.push_back(minDiff == 101 ? -1 : minDiff);
    }

    return ans;
  }
};