☑️✅Easy JAVA/C++ Solution || Beats 100%✅☑️
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Intuition\nWe will use dynammic programming to solve this question.Just go through the code(explained in code) and you 
# **PLEASE UPVOTE IF IT HELPED**\n\n---\n\n\n# Intuition\nWe will use dynammic programming to solve this question.Just go through the code(explained in code) and you will understand it.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n^2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n```Java []\nclass Solution {\n    public int helper(int []nums,int l,int r,int score,int [][]dp){\n        if(l>=r)return 0;\n        if(dp[l][r]!=-1)return dp[l][r];\n        int a1=0;\n        int a2=0;\n        int a3=0;\n        if(nums[l]+nums[l+1]==score){\n            a1=1+helper(nums,l+2,r,score,dp);\n        }if(nums[l]+nums[r]==score){\n            a2=1+helper(nums,l+1,r-1,score,dp);\n        }if(nums[r]+nums[r-1]==score){\n            a3=1+helper(nums,l,r-2,score,dp);\n        }\n        return dp[l][r]=Math.max(a1,Math.max(a2,a3));\n    }\n    public int maxOperations(int[] nums) {\n        int n=nums.length;\n        if(n<=1)return 0;\n        int score1=nums[0]+nums[1];\n        int score2=nums[n-1]+nums[n-2];\n        int score3=nums[0]+nums[n-1];\n        int [][]dp=new int[n][n];\n        for(int []i:dp){\n            Arrays.fill(i,-1);\n        }\n        int opr1=helper(nums,0,n-1,score1,dp);\n        for(int []i:dp){\n            Arrays.fill(i,-1);\n        }\n        int opr2=helper(nums,0,n-1,score2,dp);\n        for(int []i:dp){\n            Arrays.fill(i,-1);\n        }\n        int opr3=helper(nums,0,n-1,score3,dp);\n        return Math.max(opr1,Math.max(opr2,opr3));\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n     int helper(vector<int>& nums,int l,int r,int score,vector<vector<int>>&dp){\n        if(l>=r)return 0;\n        if(dp[l][r]!=-1)return dp[l][r];\n        int a1=0;\n        int a2=0;\n        int a3=0;\n        if(nums[l]+nums[l+1]==score){\n            a1=1+helper(nums,l+2,r,score,dp);\n        }if(nums[l]+nums[r]==score){\n            a2=1+helper(nums,l+1,r-1,score,dp);\n        }if(nums[r]+nums[r-1]==score){\n            a3=1+helper(nums,l,r-2,score,dp);\n        }\n        return dp[l][r]=max(a1,max(a2,a3));\n    }\n    int maxOperations(vector<int>& nums) {\n        int n=nums.size();\n        if(n<=1)return 0;\n        int score1=nums[0]+nums[1];\n        int score2=nums[n-1]+nums[n-2];\n        int score3=nums[0]+nums[n-1];\n\n        vector<vector<int>> dp(n,vector<int>(n,-1));\n        int opr1=helper(nums,0,n-1,score1,dp);   \n        \n        for(int i=0;i<n;i++){    //initializing 2d vector again with -1\n            for(int j=0;j<n;j++){\n                dp[i][j]=-1;\n            }\n        }\n        int opr2=helper(nums,0,n-1,score2,dp);\n        \n        for(int i=0;i<n;i++){        //initializing 2d vector again with -1\n            for(int j=0;j<n;j++){\n                dp[i][j]=-1;\n            }\n        }\n        int opr3=helper(nums,0,n-1,score3,dp);\n        return max(opr1,max(opr2,opr3));\n    }\n};\n```\n# **PLEASE UPVOTE IF IT HELPED**\n\n---\n\n
['Dynamic Programming', 'Memoization', 'C++', 'Java']