Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Magic Index
EASY
20 mins
124 upvotes
Binary Search
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Binary Search
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Magic Index

Contributed by
Arshit Babariya
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 80 %
Share
124 upvotes

Problem Statement

You are given a sorted array A consisting of N integers. Your task is to find the magic index in the given array.

Note :
A magic index in an array A[0 ... N - 1] is defined to be an index i such that A[i] = i.

The elements in the array can be negative.

The elements in the array can be repeated multiple times.

There can be more than one magic index in an array.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
1
5
-5 -1 2 1 9
Sample Output 1 :
2
Explanation For Sample Input 1 :
The output is 2 because A[2] = 2 and hence 2 is the magic index.
Sample Input 2 :
2
5
2 3 4 5 6
6
-1 -1 -1 4 4 4
Sample Output 2 :
-1
4
Reset Code
Full screen
Auto
copy-code
Console