Algorithm


Problem Name: 220. Contains Duplicate III

You are given an integer array nums and two integers indexDiff and valueDiff.

Find a pair of indices (i, j) such that:

  • i != j,
  • abs(i - j) <= indexDiff.
  • abs(nums[i] - nums[j]) <= valueDiff, and

Return true if such pair exists or false otherwise.

 

Example 1:

Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
Output: true
Explanation: We can choose (i, j) = (0, 3).
We satisfy the three conditions:
i != j --> 0 != 3
abs(i - j) <= indexDiff --> abs(0 - 3) <= 3
abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0

Example 2:

Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
Output: false
Explanation: After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.

 

Constraints:

  • 2 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • 1 <= indexDiff <= nums.length
  • 0 <= valueDiff <= 109

Code Examples

#1 Code Example with C Programming

Code - C Programming


typedef struct {
    int val;
    int idx;
} e_t;
int cmp(void const *a, void const *b) {
    return ((const e_t *)a)->val  <  ((const e_t *)b)->val ? -1 :
           ((const e_t *)a)->val > ((const e_t *)b)->val ?  1 :
           ((const e_t *)a)->idx < ((const e_t *)b)->idx ? -1 : 1;
}
bool containsNearbyAlmostDuplicate(int* nums, int numsSize, int k, int t) {
    e_t *p;
    int i, j, d, f;
    
    p = malloc(numsSize * sizeof(e_t));
    //assert(p);
    for (i = 0; i  <  numsSize; i ++) {
        p[i].val = nums[i];
        p[i].idx = i;
    }
    
    qsort(p, numsSize, sizeof(e_t), cmp);
    
    f = 0;
    for (i = 0; !f && i  <  numsSize - 1; i ++) {
        //printf("\n%d: %d\n", p[i].idx, p[i].val);
        for (j = i + 1;
             !f &&
             j  <  numsSize &&
              ((p[i].val > 0 && p[j].val - p[i].val <= t) ||
                p[j].val <= t + p[i].val);
             j ++) {
            //printf("%d: %d\n", p[j].idx, p[j].val);
            d = p[j].idx - p[i].idx;
            if (d  <  0) d = -d;
            if (d <= k) f = 1;
        }
    }
    
    free(p);
    
    return f;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1], indexDiff = 3, valueDiff = 0

Output

x
+
cmd
true

#2 Code Example with Javascript Programming

Code - Javascript Programming


const containsNearbyAlmostDuplicate = function(nums, k, t) {
  if (k < 1 || t < 0) {
    return false
  }
  const array = new Map()
  const num = 10 ** 10
  for (let i = 0, iL = nums.length; i  <  iL; ++i) {
    const noNegative = nums[i] + num
    const factor = Math.floor(noNegative / (t + 1))
    if (
      array.has(factor) ||
      (array.has(factor - 1) && noNegative - array.get(factor - 1) <= t) ||
      (array.has(factor + 1) && array.get(factor + 1) - noNegative <= t)
    ) {
      return true
    }
    if (array.size >= k) {
      array.delete(Math.floor((nums[i - k] + num) / (t + 1)))
    }
    array.set(factor, noNegative)
  }
  return false
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,3,1], indexDiff = 3, valueDiff = 0

Output

x
+
cmd
true

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def containsNearbyAlmostDuplicate(self, nums, k, t):
        if t < 0: return False
        d = {}
        for i in range(len(nums)):
            m = nums[i] // (t + 1)
            if m in d or (m - 1 in d and nums[i] - d[m - 1] <= t) or (m + 1 in d and d[m + 1] - nums[i] <= t):
                return True
            d[m] = nums[i]
            if i >= k: del d[nums[i - k] // (t + 1)]
        return False
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3

Output

x
+
cmd
false

#4 Code Example with C# Programming

Code - C# Programming


using System;
using System.Linq;

namespace LeetCode
{
    public class _0220_ContainsDuplicateIII
    {
        public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t)
        {
            var arr = nums.Select((num, index) => new { num, index })
                          .OrderBy(u => u.num)
                          .ToArray();

            for (int i = 0; i  <  nums.Length; i++)
                for (int j = i + 1; j  <  nums.Length && arr[j].num - (long)arr[i].num <= t; j++)
                    if (Math.Abs(arr[i].index - arr[j].index) <= k)
                        return true;

            return false;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#219 Leetcode Contains Duplicate II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#221 Leetcode Maximal Square Solution in C, C++, Java, JavaScript, Python, C# Leetcode