# Max Min

### Problem Statement :

```You will be given a list of integers,  arr , and a single integer k. You must create an array of length k from elements of arr such that its unfairness is minimized. Call that array arr' . Unfairness of an array is calculated as

max( arr' ) -  min( arr ' )

Where:
- max denotes the largest integer in arr' .
- min denotes the smallest integer in arr'.

Note: Integers in  may not be unique.

Function Description

Complete the maxMin function in the editor below.
maxMin has the following parameter(s):

int k: the number of elements to select
int arr[n]:: an array of integers
Returns

int: the minimum possible unfairness

Input Format

The first line contains an integer n, the number of elements in array arr.
The second line contains an integer k .
Each of the next n lines contains an integer arr[ i ] where 0  <=  i  < n.

Constraints

2  <=  n  <=  10^5
2  <=  k  <=  n
0   < =   arr[ i ]   <=  10^9

Sample Input 0

7
3
10
100
300
200
1000
20
30

Sample Output 0

20```

### Solution :

```                            ```Solution in C :

In  C :

#include <stdio.h>
#include<stdlib.h>
int compare(int *a,int *b)
{
return *(int*)a-*(int*)b;
}
int main(void)
{
int n,k,i,j,min,a;
scanf("%d",&n);
scanf("%d",&k);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
qsort(a,n,sizeof(int),compare);

min=a[k-1]-a;
for(i=0;i<=n-k;i++)
{
if( (j=(a[i+k-1]-a[i]) ) <min)
min=j;
}
printf("%d",min);
return 0;
}```
```

```                        ```Solution in C++ :

In  C ++ :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int arr;

int main() {

int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)cin>>arr[i];
sort(arr,arr+n);
int ans=1e9;
for(int i=k-1;i<n;i++){
ans=min(arr[i]-arr[i-k+1],ans);
}
cout<<ans<<endl;
return 0;
}```
```

```                        ```Solution in Java :

In  Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), k = in.nextInt();
int[] x = new int[n];
for(int i = 0; i < n; i++) x[i] = in.nextInt();
Arrays.sort(x);
System.out.println(f(n, k, x));
}

private static int f(int n, int k, int[] x){
int min = 100000000;
for(int i = 0; i + k-1 < x.length; i++){
if(x[i+k-1] - x[i] < min) min = x[i+k-1]-x[i];
}
return min;
}
}```
```

```                        ```Solution in Python :

In  Python3 :

def unfairness(candies, i, j):
res = candies[j-1] - candies[i]
return res

n = int(input())
kids = int(input())
candies = []

for i in range(n):
candies.append(int(input()))

candies = sorted(candies)

min_uf = unfairness(candies, 0, kids)

for i in range(1, len(candies)-kids):
this_uf = unfairness(candies, i, i+kids)
min_uf = min(min_uf, this_uf)

print(min_uf)```
```

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and