How to memoize a void function?

Revision en1, by VibhuSpeediest, 2024-10-27 22:10:31

If I've written a function with a void return type and want to memoize it, how should I implement it?

void rec(int i, int flag, int sum, int& maxi, int n, vector<int>& arr, int count) {
        if (i == n) {
            if (count <= 4) {
                maxi = max(maxi, sum);
            }
            return;
        }
        if (flag) {
            rec(i + 1, 0, sum - arr[i], maxi, n, arr, count + 1);
            rec(i + 1, 1, sum, maxi, n, arr, count);
        } else {
            rec(i + 1, 1, sum + arr[i], maxi, n, arr, count + 1);
            rec(i + 1, 0, sum, maxi, n, arr, count);
        }
    }

How can I memoize this code?

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English VibhuSpeediest 2024-10-27 22:10:31 717 Initial revision (published)