You are given an integer array 'ARR' of size βNβ and two integers βAβ and βBβ. You need to find if there are two distinct indices in the array, such that the absolute difference of values on those indices is less than or equal to βBβ and the absolute difference of the indices is less than or equal to βAβ.
Note :Can you try to solve it in O(N) time ?
The first line contains an integer βTβ which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains three space-separated integers βNβ, βAβ and βBβ, denoting the number of elements in the array, and the two integers.
The next line contains 'N' space-separated integers denoting an array of βNβ elements.
Output Format :
For each test case, print "True" if a pair of distinct indices satisfying the above conditions exist in the given array. Otherwise, print "False".
Print the output of each test case in a separate line.
Note :
You donβt need to print anything; It has already been taken care of.
1 <= T <= 100
1 <= N <= 10^4
1 <= A <= 10^4
0 <= B <= 10^9
-10^9 <= ARR[i] <= 10^9
Where βTβ is the number of test cases, βNβ, denotes the number of elements in the array, βAβ and βBβ denotes the given two integers, and 'ARR[i]' denotes the 'i'th' element of the array 'ARR'.
Time limit: 1 sec
2
4 3 0
2 3 4 2
4 1 0
5 4 2 4
True
False
In the first test case,
For indices 0 and 3, abs(0 - 3) = 3, which is equal to A and values at those indices 2 and 2, abs(2 - 2) = 0, which is equal to B, hence the answer is True in this case.
In the second test case,
There exist no pair of indices satisfying the above conditions, hence the answer is False in this case.
2
4 1 2
2 1 2 2
6 2 3
2 6 10 2 6 10
True
False
Can you think of checking each pair of indices one by one?
Approach: The basic approach to this question will be to simply use two loops and check for each possible pair of indices if it follows the given rule or not.
Algorithm:
O(N^2), where N is the number of elements in the array.
Since we are traversing through the complete array using two loops. Hence, the overall Time complexity is O(N^2).
O(1).
Constant extra space is required. Hence, the overall Space Complexity is O(1).