✅100 beats java solution with full stepwise explanations || Must watch for beginners
Intuition\n Describe your first thoughts on how to solve this problem. \nTo create a mountain we need a smaller value form left and a smaller value from right. 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo create a mountain we need a smaller value form left and a smaller value from right. \nAnd our main goal is get minimum sum.\nSo take smallest possible value from left and smallest possible value from right.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake leftmin array which will store smallest value form left curr indx and take rightmin array to store smallest value form right.\nNow check for min sum for every element.\n\nStep 1:Initialization:\n\nn is the length of the input array nums.\nTwo arrays, lmin and rmin, are initialized to store the minimum values to the left and right of each element.\n**lmin[0]** is initialized to a large value (10000).\n**rmin[n-1]** is initialized to a large value (10000).\n\nStep 2:Left Minimum Array (lmin):\n\nIterate through the array from left to right (starting from index 1).\nFor each index i, store the minimum value between the current minimum (lmin[i-1]) and the element to the left of i (nums[i-1]) in the lmin array.\n\nStep 3:Right Minimum Array (rmin):\n\nIterate through the array from right to left (starting from the second-to-last index).\nFor each index i, store the minimum value between the current minimum (rmin[i+1]) and the element to the right of i (nums[i+1]) in the rmin array.\n\nStep 4:Finding Minimum Sum:\n\nInitialize a variable ans to 10000.\nIterate through the array from the second to the second-to-last index\n **(for(int i=1; i<n-1; i++))**.\nCheck if the element at index i **(nums[i])** is greater than both the left minimum **(lmin[i])** and the right minimum **(rmin[i])**.\nIf the condition is met, update ans to the minimum of its current value and the sum of the three elements** (nums[i] + lmin[i] + rmin[i])**.\n\nStep 5:Final Result:\n\nIf ans remains unchanged (still 10000), return -1 as no valid sum meeting the conditions was found.\nOtherwise, return the computed minimum sum (ans).\n\n\n\nPlease upvote if you liked my solution.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minimumSum(int[] nums) {\n        int n=nums.length;\n        int lmin[]=new int[nums.length];\n        int rmin[]=new int[nums.length];\n        lmin[0]=10000;\n        for(int i=1;i<n;i++){\n            lmin[i]=Math.min(lmin[i-1],nums[i-1]);\n        }\n        rmin[n-1]=10000;\n        for(int i=n-2;i>=0;i--){\n            rmin[i]=Math.min(rmin[i+1],nums[i+1]);\n        }\n        int ans=10000;\n        for(int i=1;i<n-1;i++){\n            if(nums[i]>lmin[i]&&nums[i]>rmin[i]){\n                ans=Math.min(ans,nums[i]+lmin[i]+rmin[i]);\n            }\n        }\n        if(ans==10000) return -1;\n        return ans;\n    }\n        \n}\n```
['Java']