Find the point on X | C ++ Program to implement the above approach ; Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLeastDist ( int A [ ] , int N ) { sort ( A , A + N ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } int main ( ) { int A [ ] = { 4 , 1 , 5 , 10 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << " ( " << findLeastDist ( A , N ) << " , ▁ " << 0 << " ) " ; return 0 ; }