Find the point on X | Java 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
import java . util . * ; class GFG { static int findLeastDist ( int A [ ] , int N ) { Arrays . sort ( A ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } public static void main ( String [ ] args ) { int A [ ] = { 4 , 1 , 5 , 10 , 2 } ; int N = A . length ; System . out . print ( " ( " + findLeastDist ( A , N ) + " , ▁ " + 0 + " ) " ) ; } }