FastPrepFind the Smallest Magic Index

Find the Smallest Magic Index

ZipRecruiter logoZipRecruiter● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

An index i is magic when values[i] == i using zero-based indexing.

Return the smallest magic index, or -1 when no such index exists.

Function

smallestMagicIndex(values: int[]) → int

Examples

Example 1

values = [-1,0,2,5]return = 2

Index two is the first position equal to its value.

Example 2

values = [1,2,3]return = -1

No position equals its zero-based index.

Constraints

  • 0 <= values.length <= 100000
  • -1000000000 <= values[i] <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int smallestMagicIndex(int[] values) {
    // Write your code here.
}
values[-1,0,2,5]
expected2
Checking account…