<\/span><\/h2>\n\n\n\nIn this challenge, we practice calculating quartiles<\/em>.<\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nGiven an array, arr<\/strong><\/em>, of n<\/em><\/strong> integers, calculate the respective first quartile (Q<\/em>1<\/sub><\/strong>), second quartile (Q<\/em>2<\/sub><\/strong>), and third quartile (Q<\/em>3<\/sub><\/strong>). It is guaranteed that Q<\/em>1<\/sub><\/strong>, Q<\/em>2<\/sub><\/strong>, and Q<\/em>3<\/sub><\/strong> are integers.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\narr<\/em> = [9, 5, 7, 1, 3]<\/strong>
The sorted array is [1, 3, 5, 7, 9]<\/strong> which has an odd number of elements. The lower half consists of [1, 3]<\/strong>, and its median is 1+3\/2 = 2<\/strong>. The middle element is 5<\/strong> and represents the second quartile. The upper half is [5, 7]<\/strong> and its median is 5+7\/2 = 6<\/strong>. Return [2, 5, 8]<\/strong>.<\/p>\n\n\n\narr<\/em> = [1, 3, 5, 7]<\/strong>
The array is already sorted. The lower half is [1, 3]<\/strong> with a median = 1+3\/2 = 2<\/strong>. The median of the entire array is 3+5\/2 = 4<\/strong>, and of the upper half is 5+7\/2 = 6<\/strong>. Return [2, 4, 6]<\/strong>.<\/p>\n\n\n\nFunction Description<\/strong><\/p>\n\n\n\nComplete the quartiles<\/em> function in the editor below.<\/p>\n\n\n\nquartiles<\/em> has the following parameters:<\/p>\n\n\n\n- int arr[n]:<\/em> the values to segregate<\/li><\/ul>\n\n\n\n
Returns<\/strong><\/p>\n\n\n\n- int[3]:<\/em> the medians of the left half of arr<\/strong><\/em>, arr<\/strong><\/em> in total, and the right half of arr<\/strong><\/em>.<\/li><\/ul>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer, n<\/em><\/strong>, the number of elements in arr<\/strong><\/em>.
The second line contains n<\/em><\/strong> space-separated integers, each an arr<\/strong><\/em>[i<\/em>]<\/strong>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 5 <= n<\/em> <= 50<\/strong><\/li>
- 0 < arr[i] <= 100<\/strong>, where arr<\/strong><\/em>[i<\/em>]<\/strong> is the ith<\/sup><\/strong><\/em> element of the array.<\/li><\/ul>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nSTDIN Function\n----- -------- \n9 arr[] size n = 9 \n3 7 8 5 12 14 21 13 18 arr = [3, 7, 8, 5, 12, 14, 21, 13,18]<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n6\n12\n16<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\narrsorted <\/sub><\/em>= [3, 5, 7, 8, 12, 13, 14, 18, 21]<\/strong>. There is an odd number of elements, and the middle element, the median, is 12<\/strong>.<\/p>\n\n\n\nAs there are an odd number of data points, we do not include the median (the central value in the ordered list) in either half:<\/p>\n\n\n\n
Lower half (L): 3, 5, 7, 8\n\nUpper half (U): 13, 14, 18, 21<\/code><\/pre>\n\n\n\n<\/span>Solution – Day 1: Quartiles Solution<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\nint quad(vector<int> a)\n{\n int q, n=a.size(); \n \n if(n%2==0) \n q = (a[n\/2-1] + a[n\/2])\/2;\n else \n q = a[n\/2];\n \n return q;\n}\n\nint main() \n{\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n \n int n; cin>>n; vector<int >a(n);\n \n for(auto i=0; i<n; i++)\n cin>>a[i];\n \n sort(a.begin(), a.end());\n \n int Q1, Q2, Q3;\n \n Q2 = quad(a);\n \n vector<int> l(a.begin(), a.begin()+n\/2);\n vector<int> h;\n \n if(n%2==0)\n {\n h.insert(h.end(), a.begin()+n\/2, a.end());\n }\n else\n {\n h.insert(h.end(), a.begin()+n\/2+1, a.end());\n }\n \n Q1 = quad(l);\n \n Q3 = quad(h);\n \n cout<<Q1<<\"\\n\"<<Q2<<\"\\n\"<<Q3<<\"\\n\";\n \n return 0;\n}<\/pre>\n\n\n\n