Queries with Fixed Length

  • + 0 comments

    C++20

    vector<int> solve(vector<int> arr, vector<int> queries) {
        vector<int> results;
        int N = arr.size();
        for(auto q : queries){
            auto it = max_element(arr.begin()+ 0, arr.begin() + 0 + q );
            int max_e = *it;
            int min_qu = *it;
            cout << max_e << ", ";
            for(int i=1; i<N-q+1; i++){
                if(arr[i+q-1] > max_e) max_e = arr[i+q-1];
                if(arr[i-1] == max_e){
                    auto it = max_element(arr.begin()+ i, arr.begin() + i + q );
                    max_e = *it;
                }
                            
                cout << max_e << ", ";
                min_qu = min(min_qu, max_e);
            }
            
            results.push_back(min_qu);
        }
        return results;
    }