Find Magic Index in Sorted Array

Given a sorted array A consisting of N integers, your task is to find the magic index in the given array, where the magic index is defined as an index i such that A[i] = i.

Example:

Input:
T = 1
N = 5
A = [-1, 0, 1, 3, 5]
Output:
3
Explanation:

For the given array, A[3] = 3, which means index 3 is a magic index.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • -109 <= A[i] <= 109

Note:

A magic index is an index such that A[i] = i.
The elements in the array can be negative and can repeat.
There can be more than one magic index; you can return any of them.
If no magic index exists, return -1.
You do not need to print anything; just implement the provided function.
Be the first one to answer
Add answer anonymously...
Info Edge Front end 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