Check Indices With Given Difference
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 ?
Input Format :
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.
Constraints :
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
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...
Top IEO Makers Fablab Web Developer interview questions & answers
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