Contest Video Soluiton | Explanation With Drawings | In Depth | C++ | Java
Intuition, approach and complexity explained in video solution\nhttps://youtu.be/wklPGihVQRY\n\n# Code\nC++\n\nclass Solution {\npublic:\n    int minimumSum(vec
# Intuition, approach and complexity explained in video solution\nhttps://youtu.be/wklPGihVQRY\n\n# Code\nC++\n```\nclass Solution {\npublic:\n    int minimumSum(vector<int>& nums) {\n        int sz = nums.size();\n        vector<int> minLeft(sz, 0), minRight(sz, 0);\n        for(int indx = 0; indx < sz; indx++){\n            minLeft[indx] = min((indx - 1 > -1) ? minLeft[indx-1] : INT_MAX, nums[indx]);\n        }\n        for(int indx = sz - 1; indx >= 0; indx--){\n            minRight[indx] = min((indx + 1 < sz) ? minRight[indx+1] : INT_MAX, nums[indx]);\n        }\n        int minSum = INT_MAX;\n        for(int indx = 1; indx < sz-1; indx++){\n            if(minLeft[indx - 1] >= nums[indx] || minRight[indx + 1] >= nums[indx])continue;\n            int leftValue = minLeft[indx - 1];\n            int rightValue = minRight[indx + 1];\n            minSum = min(minSum, nums[indx] + rightValue + leftValue);\n        }\n        return minSum == INT_MAX ? - 1 : minSum;\n    }\n};\n```\nJava\n```\nclass Solution {\n    public int minimumSum(int []nums) {\n        int sz = nums.length;\n        int minLeft[] = new int[sz];\n        int minRight[] = new int[sz];\n        for(int indx = 0; indx < sz; indx++){\n            minLeft[indx] = Math.min((indx - 1 > -1) ? minLeft[indx-1] : Integer.MAX_VALUE, nums[indx]);\n        }\n        for(int indx = sz - 1; indx >= 0; indx--){\n            minRight[indx] =  Math.min((indx + 1 < sz) ? minRight[indx + 1] : Integer.MAX_VALUE, nums[indx]);\n        }\n        int minSum = Integer.MAX_VALUE;\n        for(int indx = 1; indx < sz-1; indx++){\n            if(minLeft[indx-1] >= nums[indx] || minRight[indx+1] >= nums[indx])continue;\n            int leftValue = minLeft[indx - 1];\n            int rightValue = minRight[indx+1];\n            minSum = Math.min(minSum, nums[indx] + rightValue + leftValue);\n        }\n        return minSum == Integer.MAX_VALUE ? - 1 : minSum;\n    }\n}\n```\n\n
['C++', 'Java']