Brute force and Optimized solution.
Intuition\nIntuition is simple I need three index i,j and k such that i<j<k and \nnums[i]nums[k]. The brute force way to solve is by running three nested loops.
# Intuition\nIntuition is simple I need three index i,j and k such that i<j<k and \nnums[i]<nums[j] and nums[j]>nums[k]. The brute force way to solve is by running three nested loops.Inside three loops i am just checking the condition and updating the result.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach :- Brute Force\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n^3)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumSum(vector<int>& nums) {\n        int res=INT_MAX,n=nums.size();\n        for(int i=0;i<n-2;i++) {\n            for(int j=i+1;j<n-1;j++) {\n                for(int k=j+1;k<n;k++) {\n                    if(nums[i]<nums[j] && nums[k]<nums[j]) {\n                        res = min(res, nums[i]+nums[j]+nums[k]);\n                    }\n                }\n            }\n        }\n        if(res==INT_MAX)\n            return -1;\n        return res;\n    }\n};\n//******************************************************************//\n# Approach: Better solution\n\n# Complexity:\n\n- Time Complexity:-O(n) traversing three time through the array.\n- Space Complexity:-O(n) because I am using 2 arrays\n# Code```\n\nclass Solution {\npublic:\n    int minimumSum(vector<int>& nums) {\n        int n = nums.size(),res=INT_MAX;\n        vector<int> lMin(n),rMin(n);\n        lMin[0]=nums[0],rMin[n-1]=nums[n-1];\n        for(int i=1;i<n;i++) {\n            lMin[i]=min(lMin[i-1],nums[i]);   \n        }\n        for(int i=n-2;i>=0;i--) {\n            rMin[i]=min(rMin[i+1],nums[i]);\n        }\n        for(int i=1;i<n-1;i++) {\n            if(lMin[i]<nums[i] && rMin[i]<nums[i]) {\n                res = min(res,lMin[i]+rMin[i]+nums[i]);\n            }\n        }\n        if(res==INT_MAX)\n            return -1;\n        return res;\n    }\n};\n```
['C++']