EASY JAVA SOLUTION USING BRUTE FORCE BEATING 100% IN 1ms
IntuitionThe problem requires identifying a mountain triplet where the middle element is the highest, and the elements on both sides are smaller. To find the mi
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires identifying a mountain triplet where the middle element is the highest, and the elements on both sides are smaller. To find the minimum sum of such a triplet, we must efficiently track valid values on the left and right of each peak. By ensuring that a valid mountain structure exists, we determine the smallest possible sum among all valid triplets. If no mountain triplet exists, we return -1.
# Approach
<!-- Describe your approach to solving the problem. -->
1. **Iterate Over i (Left Element):**
    - Start with i = 0 and go up to n - 2.
    - This ensures we have space for j and k.

1. **Iterate Over j (Middle Element):**
    - For each i, iterate j = i + 1 to n - 1.
    - Ensure nums[i] < nums[j], satisfying the mountain condition.

1. **Iterate Over k (Right Element):**
    - For each j, iterate k = j + 1 to n.
    - Ensure nums[k] < nums[j], confirming a valid mountain shape.

1. **Track the Minimum Valid Sum:**
    - Compute sum = nums[i] + nums[j] + nums[k].
    - Update minSum whenever a valid triplet is found.
    - If no valid triplet exists, return -1
# Complexity
- Time complexity: O(n^3)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
![image.png](https://assets.leetcode.com/users/images/24e3662a-a540-4270-b405-bad72964c155_1743567898.620232.png)

# Code
```java []
class Solution {
    public int minimumSum(int[] nums) {
        int n = nums.length;
        int minSum = Integer.MAX_VALUE; // Track the minimum sum
        boolean found = false;

        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                if (nums[i] < nums[j]) { // Valid i < j condition
                    for (int k = j + 1; k < n; k++) {
                        if (nums[k] < nums[j]) { // Valid k > j condition
                            int sum = nums[i] + nums[j] + nums[k];
                            minSum = Math.min(minSum, sum);
                            found = true;
                        }
                    }
                }
            }
        }

        return found ? minSum : -1;
    }
}

```
['Array', 'Two Pointers', 'Java']