PROBLEM LINK:
Author: Hari Vege
DIFFICULTY:
Easy
PREREQUISITES:
Sorting different rows of 2d array
PROBLEM:
Finding minimum difference between minimum and maximum elements when selecting k elements in each row of 2d array
EXPLANATION:
Just sorting individual rows of 2d array and finding minimum difference between minimum and maximum elements when selecting k elements in each row of 2d array
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i,j,k,l,m,p,min=9999,h,o;
cin>>i>>j;
long long int a[1000]={0},b[1000][1000]={0},s[1000]={0};
p=i;
//cout<<p;
while(p--){
cin>>k>>l;
a[l]++;
m=0;
b[l][a[l]-1]=k;
//cout<<b[l][a[l]-1]<<endl;
}
for(p=0;p<i;p++){
if(a[p]>=j){
sort(b[p]+0,b[p]+a[p]);
// for(int g=0;g<a[p];g++) cout<<b[p][g]<<" "; cout<<endl;
//cout<<j
for(o=0;o+j<=a[p];o++){
h= b[p][j+o-1]-b[p][o];
//cout<<h<<" "<<p<<" "<<o<<" "<<j+o-1<<endl;
if(min>h){
min=h;
}
}
}
}
cout<<min;
}