Check Indices With Given Difference Problem Statement
You are provided with an integer array ARR
of size N
along with two integers A
and B
. Your task is to determine if there exist two distinct indices in the array such that the absolute difference of the values at those indices is less than or equal to B
and the absolute difference of the indices is less than or equal to A
.
Strive to achieve an O(N) time complexity solution.
Input:
The first line contains an integer T
indicating the number of test cases or queries. The following details are provided for each test case:
The first line contains three space-separated integers: N
(number of elements in the array), A
, and B
.
The second line contains N
space-separated integers representing the elements of the array ARR
.
Output:
For each test case, output "True" if there exists a pair of distinct indices satisfying the conditions. Otherwise, output "False". Each result should be printed on a new line for every test case.
Example:
Input:
T = 1
N = 5, A = 3, B = 2
ARR = [1, 5, 7, 2, 6]
Output:
True
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 104
1 ≤ A ≤ 104
0 ≤ B ≤ 109
-109 ≤ ARR[i] ≤ 109
- Time limit: 1 second
Note:
No need to print anything in the function implementation as printing is handled externally.
Janavi Paratane
3mo
To solve the problem efficiently in 𝑂 ( 𝑁 ) O(N) time for each test case, we need to check whether there exist two distinct indices i and j in the array ARR such that: ∣ 𝐴 𝑅 𝑅 [ 𝑖 ] − 𝐴 𝑅 𝑅 [ 𝑗 ] ∣ ≤ ...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Web Developer
>
IEO Makers Fablab Web Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app