Given an array A[1..N] of N non-negative integers, you need to find the median of the…
- User
Given an array A[1..N] of N non-negative integers, you need to find the median of the array. The median of an array is the middle element in its sorted order. If N is even, choose the (N/2)^th element in the sorted order. Input The first line contains N, the number of integers in the array. The next line has N integers, the elements in the array
Output In a single line, output the median of the array.
Constraints
1 <= N <= 10^5 0 <= Ai <= 10^6
Example 1
Input 5 9 34 1 290 32
Output 32
Explanation The sorted order is [1, 9, 32, 34, 290]. The middle element is 32
Example 2
Input 6 17 13 10 1 3 1
Output 3
Explanation The sorted order is [1, 1, 3, 10, 13, 17]. The (N/2)^th element is 3.
Assistant
pythonn = int(input()) arr = list(map(int, input().split())) arr.sort() if n % 2 == 1: print(arr[(n - 1) // 2]) else: print(arr[(n // 2) - 1])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.