Professional Documents
Culture Documents
com
Skills Score:
Easy 105/105
Sorting 105/105
problem-solving 105/105
Recruiter/Team Comments:
No Comments.
Plagiarism flagged
We have marked questions with suspected plagiarism below. Please review it in detail here -
Problem Solving
Needs Review
QUESTION DESCRIPTION
Score 105
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
1/3
The sorted array . The middle element and the median is .
Function Description
Returns
int: the median of the array
Input Format
Constraints
is odd
Sample Input 0
7
0 1 2 4 6 5 3
Sample Output 0
Explanation 0
CANDIDATE ANSWER
1 #include <cmath>
2 #include <cstdio>
3 #include <vector>
4 #include <iostream>
5 #include <algorithm>
6 using namespace std;
7
8
9 int main() {
10
11 int n, *arr;
12 cin>>n;
13 arr = new int[n];
14 for (int i = 0 ; i < n ; i++) cin>>arr[i];
15 sort(arr,arr+n);
16 if (n%2 == 1) cout<<arr[(n-1)/2]<<endl;
17 else cout<<(arr[n/2 - 1]+arr[n/2])/2<<endl;
18 return 0;
19 }
20
21
22
23
24
2/3
25
26
27
No Comments
3/3