Two related settings: interactive problems, where you construct the answer by asking questions; and output-only or “any valid answer” problems, where a randomized or search-based construction plus a checker is acceptable.

SettingTechnique
find a hidden object with few querieseach query should split the remaining candidates (information bound )
comparisons onlytournament for the max (), max and min together ()
hidden permutation, pairwise queriesfind values by pairs, keep one “unknown” candidate
adaptive judgeyour strategy must work for every consistent answer: think of the judge as an adversary
output-only, large instanceslocal search, simulated annealing, randomized restarts, then verify
”any valid answer” with a random constructionrandom choice + checker in a loop; expected tries

Hidden permutation with mod queries

Query returns . Two queries on a pair reveal the smaller value: if , then while .

// hidden permutation, query(i, j) returns p_i mod p_j; 2 queries per step find a value
template<class Q> vector<int> guessPermutation(int n, Q query) {
    vector<int> p(n, 0); int cand = 0;             // cand: index whose value is still unknown (the current maximum candidate)
    for (int i = 1; i < n; i++) {
        int x = query(cand, i), y = query(i, cand);
        if (x > y) p[cand] = x, cand = i;          // p_cand < p_i, so p_cand mod p_i = p_cand
        else p[i] = y;
    }
    p[cand] = n;
    return p;
}

See Interactive Problems for I/O, flushing and local judges, and Heuristic Optimization for output-only search.