Hi all, newbie here. I am getting a "wrong answer" response for the following test case:
Input: [-3,3], 2, 4 Output: true Expected: false
So, according to the question:
Given an array of integers, find out whether there are two distinct
indices i and j in the array such that the difference between nums[i]
and nums[j] is at most t and the difference between i and j is at most
My interpretation of the question is that I am supposed to find a pair of element indices (i, j) whose difference is at most t=4, and the indices of these elements cannot be more than k=2 apart.
So, taking i = 0 and j = 1, I get:
nums - nums => -3 - 3 = -6
which seems to satisfy the "at most t" (t=4) requirement, since -6 is less than 4. Can the author or someone please point out my mistake in interpreting the question?
the difference "at most t" means the absolute value, |-6| > 4, so it doesn't satisfy the requirement.
One another testcase that's puzzling me is,
Input: [7,1,3], 2, 3 Output: false Expected: true
How can the correct solution to this testcase be true?
the difference between 1 and 3 is 2(<=3) and difference between their indices 1(<=2). That's why the correct answer is true
You just need to think in terms of distances between both indices and values. So in your first example: [-3,3], 2, 4. You have abs(i-j) = 1 <= 2 so that is fine, however abs(nums[i]-nums[j]) = 6 > 4 (for i=0 and j=1), so that is why the expected is false. If you take i=1 and j=0 you have abs(i-j) = 1 <= 2, yet abs(nums[i] - nums[j]) = 6 > 4.
I think that the description of this code challenge could be improved to clarify what exactly is expected. I found the description of the task unclear.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.