-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path139.array_partition.cpp
More file actions
57 lines (50 loc) · 1.18 KB
/
Copy path139.array_partition.cpp
File metadata and controls
57 lines (50 loc) · 1.18 KB
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
// Array Partition
// https://practice.geeksforgeeks.org/problems/array-partition/1#
// { Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
bool partitionArray(int N, int K, int M, vector<int> &A){
sort(A.begin(), A.end());
deque <int> q;
q.push_back(0);
for(int i=K-1; i<N; i++){
while(i-q.front()+1 >= K){
if(A[i]-A[q.front()] <= M){
q.push_back(i+1);
break;
}
q.pop_front();
if(q.empty())
return false;
}
}
return q.back() == N;
}
};
// { Driver Code Starts.
int main(){
int T;
cin >> T;
while(T--){
int N, K, M;
cin >> N >> K >> M;
vector<int> A(N);
for(int i = 0; i < N; i++){
cin >> A[i];
}
Solution obj;
bool ans = obj.partitionArray(N, K, M, A);
if(ans){
cout << "YES\n";
}
else{
cout<< "NO\n";
}
}
}
// } Driver Code Ends