12 MS C++ solution. Beat 99.84%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    struct Node {\n        size_t i;\n        size_t j;\n        int s;\n        int d;\n    };\n    typedef pair<int, int> P;\n    int maxOperations(vector<int>& nums) {\n        stack<Node> s({{0, nums.size() - 1, -1, 0}});\n        set<P> seen;\n        int result = 0;\n        while (!s.empty()) {\n            const Node f = s.top();\n            s.pop();\n            if (f.d > result) result = f.d;\n            if (f.j > f.i) {\n                const int a = nums[f.i] + nums[f.i + 1];\n                const int b = nums[f.j] + nums[f.j - 1];\n                const int c = nums[f.i] + nums[f.j];\n                vector<Node> next;\n                            \n                if ( (f.s == -1 || f.s == a)) next.push_back({f.i + 2, f.j, a, f.d + 1});\n                if ( (f.s == -1 || f.s == b) && f.j > 1) next.push_back({f.i, f.j - 2, b, f.d + 1});\n                if ( (f.s == -1 || f.s == c)) next.push_back({f.i + 1, f.j - 1, c, f.d + 1});\n\n                for (auto node : next) {\n                    P p = make_pair(node.i, node.j);\n                    if (seen.find(p) == seen.end()) {\n                        seen.insert(p);\n                        s.push(node);\n                    }\n                }\n            } else {\n                return result;\n            }\n        }\n        return result;\n    }\n};\n```
['C++']