Function optimization

Consider this function:

        int mistery(const vector<int>& A, const vector<int>& B) {
          int n = A.size();
          int res = 0;
          for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
              if (A[i] != A[j]) res = max(res, B[i] + B[j]);
          return res;
        }

Implement an alternative function, with the same name, return type and
parameters, that computes the same, but as efficiently as possible.

Precondition

Assume that @A@ and @B@ have the same number @n@ of elements, that @n@
is between 1 and 10⁵, and that the elements of the vectors are all
between 1 and 10⁸.

Observation

You only need to submit the required procedure; your main program will
be ignored.

Problem information

Author: Salvador Roura

Generation: 2026-01-25T12:04:52.695Z

© Jutge.org, 2006–2026.
https://jutge.org
