# Java O(N lg K) solution

• This problem requires to maintain a window of size k of the previous values that can be queried for value ranges. The best data structure to do that is Binary Search Tree. As a result maintaining the tree of size k will result in time complexity O(N lg K). In order to check if there exists any value of range abs(nums[i] - nums[j]) to simple queries can be executed both of time complexity O(lg K)

Here is the whole solution using TreeMap.

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums == null || nums.length == 0 || k <= 0) {
return false;
}

final TreeSet<Integer> values = new TreeSet<>();
for (int ind = 0; ind < nums.length; ind++) {

final Integer floor = values.floor(nums[ind] + t);
final Integer ceil = values.ceiling(nums[ind] - t);
if ((floor != null && floor >= nums[ind])
|| (ceil != null && ceil <= nums[ind])) {
return true;
}

if (ind >= k) {
values.remove(nums[ind - k]);
}
}

return false;
}
}

• Good use of TreeSet

• This post is deleted!

• Apparently the task is to find duplicates, so it not suppose to reach the situation that you have more then one occurrence of the given value in the tree map. Although if you can provide an example input that illustrates this situation I would be grateful.

• I agree, thank you.

• why do you set floor and ceiling final? they will change in next loop anyway.

• Although you passed the test cases. There's one issue there: overflow.
let's say nums[ind] is Integer.MAX_VALUE, t is 20. You cannot just do nums[ind] + t.

• Quite right, although the only solution that comes to my mind to solve this is only to expand the data type size from 32 bit integer to 64 bit long.

• That would not be a problem. Ceiling has already handled it.

• I think the judgement statement in the if brackets should be floor >= nums[ind] - t , ceil <= nums[ind] + t; However, it will overflow,and cannot pass all the test cases, and I think use bigger data type can solve this problem!

• Concise and Genius! Good use of TreeSet property!!!

• great solution. Have to cast to long to get it pass all test cases.(Integer overflow)

public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums.length < 2 || k == 0) {
return false;
}
TreeSet<Long> set = new TreeSet<>();

int i = 0;
while (i < nums.length) {
Long floor = set.floor((long) nums[i]);
Long ceiling = set.ceiling((long) nums[i]);
if ((floor != null && nums[i] - floor <= t ) ||
(ceiling != null && ceiling - nums[i] <= t)) {
return true;
}
if (i > k) {
set.remove((long) nums[i - k - 1]);
}
}
return false;
}

• What an elegant solution! Thank you for sharing!

• Brilliant Idea.

• i has a little question that why we start removing when ind == k,i mean at that time the least ind is 0,and the greatest is k,the difference is k.it still satisfy the rule.
I think we should hold a window which size is k+1.
Am i wrong.
the problem said "the difference between i and j is at most k.".

• how about save one ceiling operation

public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums == null || nums.length == 0 || k <= 0) {
return false;
}

final TreeSet<Long> values = new TreeSet<>();
for (int ind = 0; ind < nums.length; ind++) {

Long floor = values.floor((long)nums[ind] + t);
if (floor != null && floor + t >= nums[ind]) {
return true;
}

if (ind >= k) {
values.remove((long)nums[ind - k]);
}
}

return false;
}
}

• hi, @jmnarloch , brilliant solution ! I was originally worried about duplicates in the TreeSet and the validity of add and remove. Then I realized that if there were duplicates within k, we would have returned true already. Great implicit logic!

• Hi @kennethliaoke , what is the intuition of dropping the ceiling ? from your floor computation, i know that:

floor <= nums[ind] + t, so

floor + t <= nums[ind] + t + t,

yet so what ? could you explain that ? Thanks!

• because if floor <= nums[ind] + t and floor >= nums[ind] - t, then that's a duplicate. If floor < nums[ind] - t, there won't be a duplicate because floor is the biggest value in the treeset that is less than (nums[ind] + t). Hope it helps.

• How would ceiling handle it?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.