Advertisement

Contains Duplicate III - LeetCode 220 Solution

Contains Duplicate III - Complete Solution Guide

Contains Duplicate III is LeetCode problem 220, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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]) <= valueDi

Detailed Explanation

The problem requires us to find if there exist two distinct indices `i` and `j` in the input array `nums` such that the absolute difference between the indices is less than or equal to `indexDiff`, and the absolute difference between the values at those indices is less than or equal to `valueDiff`. In simpler terms, we need to find if there are two numbers in the array that are 'close' to each other both in terms of their value and their position in the array. The input consists of the array `nums`, the maximum allowed index difference `indexDiff`, and the maximum allowed value difference `valueDiff`. The output is a boolean value: `true` if such a pair exists, and `false` otherwise.

Solution Approach

The provided code uses a bucket sort/pigeonhole principle-based approach in conjunction with a sliding window to efficiently solve the problem. It divides the number range into buckets of size `valueDiff + 1`. For each number in the array, we determine its bucket ID. Then, we check if: (1) the bucket is already occupied (meaning a duplicate within the allowed value difference exists), (2) either the adjacent buckets contain an element within the allowed value difference. The sliding window aspect is achieved by removing the oldest element (outside the indexDiff range) from the 'buckets' data structure as we iterate through the array.

Step-by-Step Algorithm

  1. Step 1: Handle edge cases. If `indexDiff` is non-positive or `valueDiff` is negative, return `false` as no valid pairs can exist.
  2. Step 2: Initialize a hash map (buckets) to store the numbers and their corresponding bucket IDs.
  3. Step 3: Calculate the bucket size as `valueDiff + 1`.
  4. Step 4: Iterate through the array `nums` from left to right.
  5. Step 5: For each number `num` at index `i`, calculate its bucket ID as `num // bucket_size`.
  6. Step 6: Check if the current bucket ID already exists in the `buckets` hash map. If it does, return `true` (a duplicate within the allowed value difference is found).
  7. Step 7: Check if the neighboring buckets (bucket_id - 1 and bucket_id + 1) exist in the `buckets` hash map. If they do, calculate the absolute difference between the current number `num` and the numbers stored in the neighboring buckets. If any of these differences are less than or equal to `valueDiff`, return `true`.
  8. Step 8: Insert the current number `num` into the `buckets` hash map with its corresponding bucket ID.
  9. Step 9: If the current index `i` is greater than or equal to `indexDiff`, remove the oldest number from the `buckets` hash map. The oldest number is at index `i - indexDiff` and its bucket is recalculated.
  10. Step 10: If the loop completes without finding a suitable pair, return `false`.

Key Insights

  • Insight 1: Using a sliding window approach and checking all pairs within the window will result in O(n*k) time complexity (where 'k' is indexDiff), which can be too slow for large input sizes. The constraints indicate that an O(n) solution is expected.
  • Insight 2: The bucket sort/pigeonhole principle helps group nearby values into buckets, enabling efficient comparison of elements close in value. By using a bucket size of `valueDiff + 1`, any two elements within the same bucket or adjacent buckets must have a value difference less than or equal to `valueDiff`.
  • Insight 3: The sliding window is maintained by removing the element that falls out of the `indexDiff` range. This ensures that only elements within the acceptable index difference are considered.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(k)

Topics

This problem involves: Array, Sliding Window, Sorting, Bucket Sort, Ordered Set.

Companies

Asked at: Airbnb, Netflix, Palantir Technologies, Yandex.