C++ Iteration Solution (No Recursion)
Code\ncpp []\ntypedef struct {\n    int h2 = 0, ht = 0, t2 = 0; // Check if the first two (head 2), \n                                // first and last (head an
# Code\n```cpp []\ntypedef struct {\n    int h2 = 0, ht = 0, t2 = 0; // Check if the first two (head 2), \n                                // first and last (head and tail), \n                                // last two (tail 2) are calculated (\u2192visited)\n    int maxop = 0; // \u7576\u524D\u6700\u5927\u7684\u64CD\u4F5C\u6B21\u6578\n} Subarr_Calc;\n\nclass Solution {\npublic:\n    int maxOperations(vector<int>& nums) {\n        int n = nums.size();\n        if (n == 2) return 1;\n        int l, r, max_operations = 0;\n        vector<vector<Subarr_Calc>> dp(n, vector<Subarr_Calc>(n)); // \u53EA\u8A2Dn-1\u5217\u4E5F\u53EF\u4EE5AC\n        stack<pair<int, int>> st; // DFS\u7528\u7684stack\n        set<int> bases = {nums[0] + nums[1], \n                          nums[0] + nums[n-1], \n                          nums[n-2] + nums[n-1]}; // \u53BB\u9664\u91CD\u8907\u503C\n\n        for (int base : bases) {\n            for (auto& row : dp) { // \u6B78\u96F6\n                for (auto& cell : row) {\n                    cell.h2 = cell.ht = cell.t2 = 0;\n                    cell.maxop = 0;\n                } }\n            while (!st.empty())\n                st.pop();\n            st.push(make_pair(0, n-1)); // root\n\n            int prev_op;\n            while (!st.empty()) {\n                auto top = st.top();\n                l = top.first; r = top.second;\n\n                if (r-l == 1 && nums[l] + nums[r] == base) {\n                    dp[l][r].maxop = 1;\n                    st.pop();\n                    l = st.top().first; r = st.top().second;\n                    if (2 > dp[l][r].maxop) // Backtrack +1\n                        dp[l][r].maxop = 2;\n                    continue;\n                } else if (r-l <= 1) {\n                    st.pop();\n                    l = st.top().first; r = st.top().second;\n                    dp[l][r].maxop = 1;\n                    continue;\n                }\n\n                if ( !dp[l][r].h2) {\n                    if (nums[l] + nums[l+1] == base) {\n                        if (l+2 <= r)\n                            st.push(make_pair(l+2, r));\n                    }\n                    dp[l][r].h2 = 1;\n                } else if ( !dp[l][r].ht) {\n                    if (nums[l] + nums[r] == base) {\n                        if (l+1 <= r-1)\n                            st.push(make_pair(l+1, r-1));\n                    }\n                    dp[l][r].ht = 1;\n                } else if ( !dp[l][r].t2) {\n                    if (nums[r-1] + nums[r] == base) {\n                        if (l <= r-2)\n                            st.push(make_pair(l, r-2));\n                    }\n                    dp[l][r].t2 = 1;\n                } else {\n                    prev_op = dp[l][r].maxop;\n                    st.pop();\n                    if (st.empty())\n                        break;\n                    l = st.top().first; r = st.top().second;\n                    if (prev_op + 1 > dp[l][r].maxop) // Backtrack +1\n                        dp[l][r].maxop = prev_op + 1;\n                }\n            }\n            if (prev_op > max_operations)\n                max_operations = prev_op;\n        }\n        return max_operations;\n    }\n};\n```
['C++']