Search for Indices with Given Difference and Distance

You are provided with an array containing 'N' non-negative integers, along with two other non-negative integers 'K' and 'M'. Your task is to identify and return a pair of indices (i, j) from the array such that 'i' ≠ 'j', |Ai - Aj| ≤ 'M', and |i - j| ≤ 'K', where |a - b| denotes the absolute value of the difference between a and b.

Input:

The input starts with an integer 'T', representing the number of test cases.
For each test case, the first line contains three space-separated integers 'N', 'K', and 'M'.
The second line includes 'N' space-separated integers representing the elements of the array.

Output:

A function that returns a pair of indices (i, j). The output will display “valid” if the indices satisfy the conditions, otherwise it will show “invalid”.

Example:

Input:
T = 1
N = 5 K = 2 M = 3
Array = [1, 3, 6, 7, 3]

Output:
“valid”

Constraints:

  • 1 ≤ T ≤ 50
  • 2 ≤ N ≤ 104
  • 1 ≤ K ≤ N
  • 1 ≤ M ≤ 109
  • 1 ≤ ARR[i] ≤ 109
  • The array is 0-indexed, with 0 ≤ i, j < N.

Note:

No need to print any output, just implement the function based on the conditions provided. It is guaranteed that at least one valid pair of indices exists.
AnswerBot
5d

Find a pair of indices in an array with given difference and distance constraints.

  • Iterate through the array and check all possible pairs of indices to satisfy the conditions

  • Use nested loops to compare...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter