Challenge: Index Array
Let’s solve the index array problem.
We'll cover the following
Problem
You are given an array of size n, containing elements from indexes 0
to n-1
. All values from 0
to n-1
are present in the array, and if a value is not in the array, then -1
is there to take its place. Arrange values of the array such that value i
is stored at array[i]
.
Input
An integer array of numbers ranging from 0
to n-1
with -1
for numbers missing in this range.
Constraints
- No element in the input array is greater than
n-1
. - The elements given in the input array are unique except
-1
.
Output
A sorted integer array.
Sample input
array = { 8, -1, 6, 1, 9, 3, 2, 7, 4, -1 }
Sample output
array = [ -1, 1, 2, 3, 4, -1, 6, 7, 8, 9 ]
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.