Soluție HackerRank pentru Find the Median. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Find the Median
Cerinta completa
The median of a list of numbers is essentially its middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, find the median?
Example
[Expresie matematică indisponibilă în copia arhivată]
The sorted array [Expresie matematică indisponibilă în copia arhivată]. The middle element and the median is [Expresie matematică indisponibilă în copia arhivată].
Function Description
Complete the findMedian function in the editor below.
findMedian has the following parameter(s):
- int arr[n]: an unsorted array of integers
Returns
- int: the median of the array
Input Format
The first line contains the integer [Expresie matematică indisponibilă în copia arhivată], the size of [Expresie matematică indisponibilă în copia arhivată].
The second line contains [Expresie matematică indisponibilă în copia arhivată] space-separated integers [Expresie matematică indisponibilă în copia arhivată]
Constraints
- [Expresie matematică indisponibilă în copia arhivată]
- [Expresie matematică indisponibilă în copia arhivată] is odd
- [Expresie matematică indisponibilă în copia arhivată]
Sample Input 0
7
0 1 2 4 6 5 3
Sample Output 0
3
Explanation 0
The sorted [Expresie matematică indisponibilă în copia arhivată]. It’s middle element is at [Expresie matematică indisponibilă în copia arhivată].
Limbajul de programare folosit: python3
Cod:
#!/bin/python3
if __name__ == '__main__':
n = int(input().strip())
arr = list(map(int, input().split()))
arr.sort()
print(arr[n // 2])
Scor obtinut: 1.0
Submission ID: 464590744
Link challenge: https://www.hackerrank.com/challenges/find-the-median/problem
