Skip to main content

Command Palette

Search for a command to run...

Minimum Sum of Mountain Triplets I

Published
2 min read
Minimum Sum of Mountain Triplets I
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed array nums of integers.

A triplet of indices (i, j, k) is a mountain if:

  • i < j < k

  • nums[i] < nums[j] and nums[k] < nums[j]

Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.

LeetCode Problem - 2908

class Solution {
    // Method to find the minimum sum of a triplet where the elements are in increasing order of index
    public int minimumSum(int[] nums) {
        // Initialize the result variable with the maximum possible integer value
        int result = Integer.MAX_VALUE;

        // Loop through each element in the array
        for (int i = 0; i < nums.length; i++) {
            // Check the elements after the current element
            for (int j = i + 1; j < nums.length; j++) {
                // Check if the element at index i is less than the element at index j
                if (nums[i] < nums[j]) {
                    // If yes, check the elements after index j
                    for (int k = j + 1; k < nums.length; k++) {
                        // Check if the element at index k is less than the element at index j
                        if (nums[k] < nums[j]) {
                            // If yes, calculate the sum of the triplet
                            int sum = nums[i] + nums[j] + nums[k];
                            // Update the result if the current sum is less than the previous result
                            if (sum < result) {
                                result = sum;
                            }
                        }
                    }
                }
            }
        }

        // If no valid triplet is found, return -1
        if (result == Integer.MAX_VALUE)
            return -1;
        else
            return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.