-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzles.cpp
59 lines (55 loc) · 1.27 KB
/
puzzles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;
int main()
{
int n;
int m;
cin>>n>>m;
int arr[m];
for(int i=0;i<m;i++){
cin>>arr[i];
}
for(int i=0;i<m-1;i++){
for(int j=0;j<m-1;j++){
if(arr[j]>arr[j+1]){
int temp;
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
// cout<<endl;
// for(int i=0;i<m;i++){
// cout<<arr[i]<<" ";
// }
// cout<<endl;
// if(n==m) {cout<<"0";
// return 0 ;
// }
int x = m-n+1; // x = 18
int difference[x];
// for(int i=0;i<m-n;i++){
// if(arr[i+n-1]>=arr[i]) difference[i]=arr[i+n-1]-arr[i];
// else if(arr[i+n-1]<arr[i]) difference[i]=arr[i]=arr[i+n-1];
// }
for(int i=0;i<x;i++){
difference[i]=arr[i+n-1]-arr[i];
}
// for(int i=0;i<x;i++){
// cout<<difference[i]<<" ";
// }
for(int i=0;i<x;i++){
for(int j=0;j<x-1;j++){
if(difference[j]>difference[j+1]){
int temp;
temp = difference[j];
difference[j]=difference[j+1];
difference[j+1]=temp;
}
}
}
// cout<<endl;
cout<<difference[0];
return 0;
}