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.
AnswerBot
4mo

Find the magic index in a sorted array where A[i] = i.

  • Iterate through the array and check if A[i] = i for each index i.

  • Since the array is sorted, you can optimize the search using binary search.

  • Return...read more

Help your peers!
Select
Add answer anonymously...

Top Front end Developer Interview Questions Asked at Info Edge

Q. How does the React lifecycle work in functional components?
Q. Jumping Numbers Problem Statement Given a positive integer N, your goal is to fi...read more
Q. Matching Prefix Problem Statement Given an integer N representing the number of ...read more
Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits