Question Name:MAX MIN

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<cassert>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define pdn(n) printf("%d\n",n)
#define sl(n) scanf("%lld",&n)
#define sd(n) scanf("%d",&n)
#define pn printf("\n")
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
int main()
{
    vector < long long  int > arr;
    long long int n,i,ans=1000000000000000000LL,k;
    sl(n),sl(k);
    arr.resize(n);
    for(i=0; i<n; i++)
        sl(arr[i]);
    sort(arr.begin(),arr.end());
    for(i=0; i<=n-k; i++)
        if( (arr[i+k-1]-arr[i]) < ans)ans=arr[i+k-1]-arr[i];
    cout << ans << endl;
    return 0;
}
  • Problem Description
    Given a list of N integers, your task is to select K integers from the list such that its unfairness is minimized.

    if (x1,x2,x3,…,xk) are K numbers selected from the list N, the unfairness is defined as 

    max(x1,x2,..,xk)-min(x1,x2,….,xk)

    where max denotes the largest integer among the elements of K, and min denotes the smallest integer among the elements of K .

    Input Format 

    The first line contains an integer N. 
    The second line contains an integer K. 
    N lines follow. Each line contains an integer that belongs to the list N.

    Note: Integers in the N list may not be unique.

    Output Format 
    An integer that denotes the minimum possible value of unfairness.

    Constraints 
    2<=N<=10^5 
    2<=K<=N 
    0<=integer in N<=10^9
  • Test Case 1
    Input (stdin)7
    3
    10
    100
    300
    200
    1000
    20
    30
    Expected Output20
  • Test Case 2
    Input (stdin)10
    4
    1
    2
    3
    4
    10
    20
    30
    40
    100
    200
    Expected Output3

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock