Deque-STL

Sort by

recency

|

452 Discussions

|

  • + 0 comments

    A bit straightforward which passes all tests:

    void printKMax(int arr[], int n, int k) { std::deque subArr; int shift = 0; for(int a=0;a < k; ++a) subArr.push_back(arr[a]); // Get actual max value (iterator) auto maxval = std::max_element(subArr.begin(), subArr.end(), [](int a, int b){return a < b;}); // Get max value position int pos = maxval - subArr.begin(); cout << *maxval << ' ';

    while(shift + k < n){
        subArr.pop_front(); // Remove old value
        subArr.push_back(arr[shift + k]); // Add new value 
        pos--; // Move max value position back
    
        // If new value is greater than actual max
        if(subArr.back() > *maxval){
            pos = subArr.size() - 1;
            maxval = subArr.begin() + pos;
        }
        else if(pos < 0){ // If not and old value ran out of scope find new max value
            maxval = std::max_element(subArr.begin(), subArr.end(), [](int a, int b){return a < b;});
            pos = maxval - subArr.begin();
        }
    
        // Print new max value
        cout << *maxval << ' ';
        shift++;
    }
    
    cout << '\n';
    

    }

  • + 0 comments

    include

    using namespace std; void print(vector &arr, int k) { deque dq; int n = arr.size();

    for (int i = 0; i < n; ++i) {
        if (!dq.empty() && dq.front() == i - k)
            dq.pop_front();
        while (!dq.empty() && arr[dq.back()] < arr[i])
            dq.pop_back();
        dq.push_back(i);
        if (i >= k - 1)
            cout << arr[dq.front()] << " ";
    }
    cout << '\n';
    

    }

    int main() { // freopen("input.inp", "r", stdin); // freopen("output.out", "w", stdout); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; print(arr, k); } return 0; }

  • + 0 comments
    //Write your code here.
    deque<int> d(arr,arr+k); //sub array
    auto max = max_element(d.begin(),d.end()); //get max of sub array
    cout << *max <<" "; 
    
    //get max if max is removed
    if(*max  == d.front())
        max = max_element(d.begin()+1,d.end());
    
    d.pop_front(); //remove front element     
    
    for(int i=k;i<n;++i){
        d.push_back(arr[i]);
        //check max
        if(arr[i] >= *max) 
            max = d.end()-1;              
    
        cout << *max <<" ";
        //get max if max is removed
        if(max  == d.begin()){ 
            max = max_element(d.begin()+1,d.end());
        }  
        d.pop_front();      
    }
    
  • + 0 comments

    include

    include

    using namespace std;

    void printKMax(int arr[], int n, int k){ deque deq; int i=0; deque::iterator it; for (i = 0; i < k; ++i) { while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } for (; i < n; ++i) { cout << arr[deq.front()] << " "; while (!deq.empty() && deq.front() <= i - k) { deq.pop_front(); } while (!deq.empty() && arr[i] >= arr[deq.back()]) { deq.pop_back(); } deq.push_back(i); } cout << arr[deq.front()] << endl; }

    int main(){

    int t;
    cin >> t;
    while(t--) {
        int n,k;
        cin >> n >> k;
        int i;
        int arr[n];
        for(i=0;i<n;i++)
            cin >> arr[i];
        printKMax(arr, n, k);
    }
    return 0;
    

    }

  • + 0 comments

    this also exceeded the time limit

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <deque>
    using namespace std;
    
    vector<vector<int>> makesubarray(deque<int> dq, int k) {
        vector<vector<int>> V;
        for (int i = 0; i <= dq.size() - k; i++) { // Change to <= to ensure valid subarray
            vector<int> v;
            for (int j = 0; j < k; j++) {
                v.push_back(dq[i + j]);
            }
            V.push_back(v);
        }
        return V;
    }
    
    deque<int> findmax(vector<vector<int>> v) {
        deque<int> dq;
        for (auto &a : v) {
            int max = a[0]; // Initialize max with the first element of the subarray
            for (int i = 1; i < a.size(); i++) { // Start from the second element
                if (a[i] > max) {
                    max = a[i]; // Update max if current element is greater
                }
            }
            dq.push_back(max);
        }
        return dq;
    }
    
    int main() {
        int t;
        cin >> t;
        for (int i = 0; i < t; i++) {
            int n, k;
            cin >> n >> k;
    
            deque<int> dq;
            for (int j = 0; j < n; j++) {
                int element;
                cin >> element;
                dq.push_back(element);
            }
            
            for (auto &i : findmax(makesubarray(dq, k))) {
                cout << i << " "; // Add space for better formatting
            }
            cout << endl;
        }
        return 0;
    }
    

    `