-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
65 lines (55 loc) · 1.4 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
// Function for calculating
// the median
//The idea is to use nth_element() function in C++ STL.
//Time complexity O(n)
double findMedian(vector<int> a, int n)
{
// If size of the arr[] is even
if (n % 2 == 0) {
// Applying nth_element
// on n/2th index
nth_element(a.begin(),
a.begin() + n / 2,
a.end());
// Applying nth_element
// on (n-1)/2 th index
nth_element(a.begin(),
a.begin() + (n - 1) / 2,
a.end());
// Find the average of value at
// index N/2 and (N-1)/2
return (double)(a[(n - 1) / 2]
+ a[n / 2])
/ 2.0;
}
// If size of the arr[] is odd
else {
// Applying nth_element
// on n/2
nth_element(a.begin(),
a.begin() + n / 2,
a.end());
// Value at index (N/2)th
// is the median
return (double)a[n / 2];
}
}
// Driver Code
int main()
{
// number of elements
int n;
cin>>n;
// Given array arr[]
vector<int> arr;
for(int i=0; i<n; i++){
int x;
cin>>x;
arr.push_back(x);
}
// Function Call
cout << findMedian(arr, n);
return 0;
}