We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You need to find the max of the K values.
Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue.
If the value to pop is the max value, then start again and find the max of the K values.
This is not optimized and if a lot of max values are repeated the complexity will increment.
But this code pass all the tests in time.
voidprintKMax(intarr[],intn,intk){//Write your code here.deque<int>fila;deque<int>::iteratoritr;intmax=0;for(inti=0;i<n;i++){fila.push_back(arr[i]);}while(fila.size()>=k){itr=fila.begin();for(inti=0;i<k;i++){if(*itr>max)max=*itr;itr++;}cout<<max<<" ";while((fila.front()!=max)&&(itr!=fila.end())){if(max<*itr)max=*itr;cout<<max<<" ";itr++;fila.pop_front();}fila.pop_front();max=0;}cout<<endl;}
void printKMax(int arr[], int n, int k){
// //Write your code here.
// int i = 0;
// int j = k-1;
// while(jmaxi) maxi = arr[k];
// }
// cout< dq;
// Process the first 'k' elements.
for (int i = 0; i < k; i++) {
// Remove elements smaller than the current one.
while (!dq.empty() && arr[i] >= arr[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
// Process the remaining elements.
for (int i = k; i < n; i++) {
// The front of the deque is the largest element of the previous window.
cout << arr[dq.front()] << " ";
// Remove elements that are out of this window.
while (!dq.empty() && dq.front() <= i - k) {
dq.pop_front();
}
// Remove elements smaller than the current one.
while (!dq.empty() && arr[i] >= arr[dq.back()]) {
dq.pop_back();
}
dq.push_back(i);
}
// Print the maximum of the last window.
cout << arr[dq.front()] << endl;
}
int main(){
int t;
cin >> t;
while(t>0) {
int n,k;
cin >> n >> k;
int i;
int arr[n];
for(i=0;i<n;i++)
cin >> arr[i];
printKMax(arr, n, k);
t--;
}
return 0;
I had a solution which pushed and pops, but that exceeded time limits.
But the straight-forward solution below did as well. Anybody any clues?
voidprintKMax(intarr[],intn,intk){//Write your code here.std::deque<int>que;intcnt;for(cnt=0;cnt<n;cnt++)que.push_back(arr[cnt]);// just push them allfor(cnt=0;cnt<n-k;cnt++)std::cout<<*std::max_element(que.begin()+cnt,que.begin()+cnt+k)<<' ';std::cout<<*std::max_element(que.begin()+cnt,que.begin()+cnt+k)<<std::endl;}
//This is the simplest implementation of all and passes all test cases
include
include
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i>arr[i];
}
deque deq;//contains the indices of max elements of arr
for(int i=0;i
/*this checks if the i'th element is greater than the back of
deq which is the previously checked and added indice*/
while(!deq.empty()&& arr[i]>arr[deq.back()])
deq.pop_back();
deq.push_back(i);
if(i>=k-1)//this prints past the first window
cout<<arr[deq.front()]<<" ";
}
cout<<endl;
}
return 0;
void printMaxInSubarrays(const deque& dq, int k) {
deque<int> indices; // To store indices of potential maximums
for (int i = 0; i < dq.size(); ++i) {
// Remove indices that are out of the current window
while (!indices.empty() && indices.front() <= i - k) {
indices.pop_front();
}
// Remove elements that are less than the current element
while (!indices.empty() && dq[indices.back()] <= dq[i]) {
indices.pop_back();
}
// Add current element's index at the end of the deque
indices.push_back(i);
// The first k-1 windows will not be complete, so skip printing max
if (i >= k - 1) {
cout << dq[indices.front()] << " ";
}
}
cout << endl;
You need to find the max of the K values. Then while you do not will pop the max value, and not arrive the end of queue, continue comparing to the next value, printing the max and pop_front of queue. If the value to pop is the max value, then start again and find the max of the K values.
This is not optimized and if a lot of max values are repeated the complexity will increment. But this code pass all the tests in time.
include
include
using namespace std;
void printKMax(int arr[], int n, int k){ // //Write your code here. // int i = 0; // int j = k-1; // while(jmaxi) maxi = arr[k]; // } // cout< dq;
}
int main(){
}
I had a solution which pushed and pops, but that exceeded time limits. But the straight-forward solution below did as well. Anybody any clues?
//This is the simplest implementation of all and passes all test cases
include
include
using namespace std;
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t; cin>>t; while(t--) { int n,k; cin>>n>>k; int arr[n]; for(int i=0;i>arr[i]; } deque deq;//contains the indices of max elements of arr for(int i=0;i
}
include
include
include
include
using namespace std;
void printMaxInSubarrays(const deque& dq, int k) {
}
int main(){
}