# 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[100010];
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[0];
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[100010];

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)```
```

## Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element