Source Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {

    ios_base::sync_with_stdio(0);
    cout.tie(0); cout.tie(0);


    int tt;
    cin >> tt;
    while(tt--){

        int n;
        cin >> n;
        vector<int> a(n);
        for(int &x : a){
            cin >> x;
        }
        vector<pair<int,int>> v;
        for(int i = 0 ; i < n ; i++){
            int x = i + a[i] + 1;
            v.push_back(make_pair(x , i + 1));
        }

        sort(v.begin() , v.end());

        vector<int> suffix(n + 2 , 1e9);
        for(int i = n - 1 ; ~i ; i--){
            suffix[i] = min(suffix[i + 1] , v[i].second);
        }

        vector<int> vals;
        for(auto x : v) {
            vals.push_back(x.first);
        }

        vector<int> ans;
        for(int i = 0 ; i < n ; i++){
            int x = i - a[i] + 1;
            int p = lower_bound(vals.begin() , vals.end() , x) - vals.begin();
            if(p == (int)vals.size()){
                ans.push_back(-1);
            }
            else{
                ans.push_back(suffix[p]);
            }
        }
        for(int x : ans){
            cout << x << " ";
        }
        cout << '\n';
    }
    return 0;
}
Copy
Easy challenge? Greedious
GNU G++17
139 ms
7.4 MB
Accepted