757. Find Median of an Unsorted Array
Given an unsorted array of integers, your task is to return the median of the array.
The median is:
- The middle element in an odd-length sorted array.
- The average of the two middle elements in an even-length sorted array.
Example 1:
Input: arr = [2, 4, 1, 3, 5]
Output: 3
Explanation:
Sorted array = [1, 2, 3, 4, 5]. Middle element = 3.
Example 2:
Input: arr = [2, 5, 1, 7]
Output: 3.5
Explanation:
Sorted array = [1, 2, 5, 7]. Middle elements = 2 and 5. Median = (2 + 5) / 2 = 3.5.
Now Your Turn!
Pick the correct output for the given inputInput: arr = [9, -1, 4, 6, 0]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= arr.length <= 105
- -106 <= arr[i] <= 106