✅☑[C++/Java/Python/JavaScript] || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n1. The code defines a class Solution, which contains a single function minimu
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n1. The code defines a class `Solution`, which contains a single function `minimumSum` that takes a vector of integers as input.\n\n1. Two arrays, `l` and `r`, are declared with a maximum size of 100010 to store minimum values on the left and right sides of the array.\n\n1. The function starts by calculating the size of the input vector `nums` and initializes the left array `l` with the first element of `nums` and the right array `r` with the last element of `nums`.\n\n1. It then proceeds to calculate the minimum values from the left side of the array using a for loop. The left minimum at index `i` is calculated as the minimum of the left minimum at index `i-1` and the value of `nums[i]`.\n\n1. Similarly, it calculates the minimum values from the right side of the array using another for loop. The right minimum at index `i` is calculated as the minimum of the right minimum at index `i+1` and the value of `nums[i]`.\n\n1. The code initializes a variable `mn` with a large value (2e9), which is used to track the minimum sum. It\'s initialized with a large value to ensure any valid sum is smaller.\n\n1. The code then iterates through the elements of the array, excluding the first and last elements, using a for loop. This is because the first and last elements don\'t have both left and right elements.\n\n1. For each element at index `i`, it checks if the element is smaller than both its left and right minimums. If so, it proceeds to calculate a potential sum.\n\n1. The potential sum is calculated as `l[i] + nums[i] + r[i]`, where `l[i]` is the minimum value on the left of the element, `nums[i]` is the current element, and `r[i]` is the minimum value on the right of the element.\n\n1. If the potential sum is smaller than the current minimum sum `mn`, `mn` is updated with the potential sum.\n\n1. Finally, the code returns the minimum sum `mn`. If no valid sum is found (i.e., `mn` remains at its initialized value), it returns -1.\n\n\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution \n{\npublic:\n    int l[100010], r[100010]; // Declare two arrays to store minimum values on the left and right.\n\n    int minimumSum(vector<int>& nums) \n    {\n        int n = nums.size(); // Get the size of the input vector.\n        l[0] = nums[0]; // Initialize the left array with the first element of nums.\n        r[n-1] = nums[n-1]; // Initialize the right array with the last element of nums.\n\n        // Calculate the minimum values from the left side of the array.\n        for(int i = 1; i < n; i ++)\n            l[i] = min(l[i-1], nums[i]);\n\n        // Calculate the minimum values from the right side of the array.\n        for(int i = n-2; i >= 0; i --)\n            r[i] = min(r[i+1], nums[i]);\n\n        int mn = 2e9; // Initialize mn with a large value to track the minimum sum.\n\n        // Iterate through the array elements except the first and last.\n        for(int i = 1; i < n-1; i ++)\n        {\n            // Check if the current element is smaller than its left and right minimums.\n            if(l[i] < nums[i] && r[i] < nums[i])\n                mn = min(mn, l[i] + nums[i] + r[i]); // Update mn if the sum is smaller.\n        }\n\n        return (mn==2e9) ? -1 : mn; // Return the minimum sum if found, otherwise -1.\n    }\n};\n\n```\n\n\n```C []\n\n#define min(a, b) ((a) < (b) ? (a) : (b))\n\nint minimumSum(int* nums, int numsSize) {\n    int l[100010], r[100010];\n    int n = numsSize;\n\n    // Initialize the left and right arrays with the first and last elements of nums.\n    l[0] = nums[0];\n    r[n - 1] = nums[n - 1];\n\n    // Calculate the minimum values from the left side of the array.\n    for (int i = 1; i < n; i++)\n        l[i] = min(l[i - 1], nums[i]);\n\n    // Calculate the minimum values from the right side of the array.\n    for (int i = n - 2; i >= 0; i--)\n        r[i] = min(r[i + 1], nums[i]);\n\n    int mn = 2e9; // Initialize mn with a large value to track the minimum sum.\n\n    // Iterate through the array elements except the first and last.\n    for (int i = 1; i < n - 1; i++) {\n        // Check if the current element is smaller than its left and right minimums.\n        if (l[i] < nums[i] && r[i] < nums[i])\n            mn = min(mn, l[i] + nums[i] + r[i]); // Update mn if the sum is smaller.\n    }\n\n    return (mn == 2e9) ? -1 : mn; // Return the minimum sum if found, otherwise -1.\n}\n\n\n```\n\n```Java []\npublic class Solution {\n    public int minimumSum(int[] nums) {\n        int n = nums.length;\n        int[] l = new int[n];\n        int[] r = new int[n];\n\n        l[0] = nums[0];\n        r[n - 1] = nums[n - 1];\n\n        for (int i = 1; i < n; i++) {\n            l[i] = Math.min(l[i - 1], nums[i]);\n        }\n\n        for (int i = n - 2; i >= 0; i--) {\n            r[i] = Math.min(r[i + 1], nums[i]);\n        }\n\n        int mn = 2_000_000_000;\n\n        for (int i = 1; i < n - 1; i++) {\n            if (l[i] < nums[i] && r[i] < nums[i]) {\n                mn = Math.min(mn, l[i] + nums[i] + r[i]);\n            }\n        }\n\n        return (mn == 2_000_000_000) ? -1 : mn;\n    }\n}\n\n```\n\n\n```python3 []\nclass Solution:\n    def minimumSum(self, nums: List[int]) -> int:\n        n = len(nums)\n        l = [0] * n\n        r = [0] * n\n\n        l[0] = nums[0]\n        r[n - 1] = nums[n - 1]\n\n        for i in range(1, n):\n            l[i] = min(l[i - 1], nums[i])\n\n        for i in range(n - 2, -1, -1):\n            r[i] = min(r[i + 1], nums[i])\n\n        mn = 2_000_000_000\n\n        for i in range(1, n - 1):\n            if l[i] < nums[i] and r[i] < nums[i]:\n                mn = min(mn, l[i] + nums[i] + r[i])\n\n        return -1 if mn == 2_000_000_000 else mn\n\n```\n\n\n```javascript []\nvar minimumSum = function(nums) {\n    const n = nums.length;\n    const l = new Array(n);\n    const r = new Array(n);\n\n    l[0] = nums[0];\n    r[n - 1] = nums[n - 1];\n\n    for (let i = 1; i < n; i++) {\n        l[i] = Math.min(l[i - 1], nums[i]);\n    }\n\n    for (let i = n - 2; i >= 0; i--) {\n        r[i] = Math.min(r[i + 1], nums[i]);\n    }\n\n    let mn = 2_000_000_000;\n\n    for (let i = 1; i < n - 1; i++) {\n        if (l[i] < nums[i] && r[i] < nums[i]) {\n            mn = Math.min(mn, l[i] + nums[i] + r[i]);\n        }\n    }\n\n    return mn === 2_000_000_000 ? -1 : mn;\n};\n\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Two Pointers', 'C', 'C++', 'Java', 'Python3', 'JavaScript']