# Median of an Array in Java

In a sorted (ordered) array, the mid-value is
the median. The median is the total of
the middle two integers divided by two if the array has an even number of
elements. The median is a more accurate indicator of the group's central
tendency since it is not influenced by unusually high or low characteristic
values. For Example mean of 1, 2, and 1000 is 334 but, median is 3. In mean,
higher value (1000) influenced the results. Get an
array of Integers *arr[]* with size *n* and find the median value of *arr[]*

Assume your
array inputs are unsorted.

Example

Input 1:

5

5 2 3 1 4

Output 1:

3

Explanation:

(3 is the middle element in [1 2 3 4 5] )

Input 2:

6

5 2 6 3 1 7

Output 1:

4

Explanation:

(3 and 5 are
the middle elements in [1 2 3 5 6 7] and average of 3 and 5 is 4)

#### Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#### Program or Solution

```
```
import java.util.*;

class Median

{

public static void main(String args[])

{

**//Variable and Object Declarations**

Scanner input = new Scanner(System.in);

int n;

double median;

**//Getting size of Array & Declare Array arr[]**

n = input.nextInt();

int arr[] = new int[n];

**//Get n values to array arr[]**

for(int i = 0; i<n; i++)

{

arr[i] = input.nextInt();

}

**//sort the array before taking middle value**

Arrays.sort(arr);

if(n%2==0)

{

//Median for Even number of Elements

median = (double)(arr[n/2-1] + arr[n/2]) / 2;

}

else

{

** ****//Median for ODD number of Elements**

median = arr[n/2];

}

System.out.print(median);

}

}