Optimal Power Line Location Power distribution company is pl
Solution
Code:
package cmsc401;
import java.util.Scanner;
public class Cmsc401 {
// function to call quick sort
public static void sorting(int[] a)
{
quick_sort_array(a, 0, a.length - 1);
}
// recursive funnction quick sort
public static void quick_sort_array(int a[], int l, int h)
{
int i = l, j = h;
int tmp;
// find pivot element
int pivot = a[(l + h) / 2];
//partition the array
while (i <= j)
{
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j)
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
}
if (l < j)
quick_sort_array(a, l, j);
if (i < h)
quick_sort_array(a, i, h);
}
public static void main(String[] args)
{
Scanner s = new Scanner( System.in );
int n=s.nextInt();
int[] a=new int[n];
for(int i=0;i<n ;i++)
{
a[i]=s.nextInt();
}
// call sorting function
sorting(a);
int num;
// if total number of elements are even
if (n%2==0)
{
// if sum of two middle elements is even take half of it
if((a[n/2]+a[n/2-1])%2==0)
{
num=(a[n/2]+a[n/2-1])/2;
}
// if not even add 1 to it to get upper bound element
else
num=(a[n/2]+a[n/2-1])/2+1;
}
// otherwise take the middle element
else
{
num=a[n/2+1];
}
System.out.println(num);
}
}
Output:
6
1
8
3
6
2
7
5

