LeetCode September Challenge(Day-2)

Asish Raz
2 min readSep 9, 2020
LeetCode

Topic — Contains Duplicate III

Q. Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3, t = 0
Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1, t = 2
Output: true

Example 3:

Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false

Constraints:

  • 0 <= nums.length <= 2 * 104
  • -231 <= nums[i] <= 231 - 1
  • 0 <= k <= 104
  • 0 <= t <= 231 - 1

Logic:

  • First, iterate over the list by using enumerate function to get the element and the index of that particular element
  • Sort the new list according to the index value of the element
  • Since the first condition in the question is to check whether the absolute difference between 2 elements is at most ‘t’ or not, therefore we have to check the first element of the new list(which is list of tuples)
  • And then the second condition is to check whether the absolute difference between 2 indices is at most ‘k’ or not, therefore we have to check the second elements of the new list(which is list of tuples)
  • If both the condition are satisfied then we can return true otherwise false

Code:

code

Happy Coding!!!

Here is the leetcode link:
https://leetcode.com/explore/challenge/card/september-leetcoding-challenge/554/week-1-september-1st-september-7th/3446/

Please feel free to comment, if you have any problem or doubt.

Thank You!!!
Never settle and always Hustle!!!

-Gareeb CODER

(Thanks for your time and do encourage me to write more by clapping.)

--

--

Asish Raz

entrepreneur, moody, moody writer, moody singer, traveller, hangout lover