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!
Add answer anonymously...
Info Edge Front end Developer interview questions & answers
A Front end Developer was asked Q. How does the React lifecycle work in functional components?
A Front end Developer was asked Q. Jumping Numbers Problem Statement Given a positive integer N, your goal is to fi...read more
A Front end Developer was asked Q. Find Magic Index in Sorted Array Given a sorted array A consisting of N integers...read more
Popular interview questions of Front end Developer
A Front end Developer was asked Q1. How does the React lifecycle work in functional components?
A Front end Developer was asked Q2. Jumping Numbers Problem Statement Given a positive integer N, your goal is to fi...read more
A Front end Developer was asked Q3. Find Magic Index in Sorted Array Given a sorted array A consisting of N integers...read more
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

