Skip to main content

Command Palette

Search for a command to run...

Valid Mountain Array

Published
2 min read
Valid Mountain Array
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.

Given an array of integers arr, return true if and only if it is a valid mountain array.

Recall that arr is a mountain array if and only if:

LeetCode Problem - 941

class Solution {
    public boolean validMountainArray(int[] arr) {
        // Check if the array has at least three elements
        if (!(arr.length >= 3)) return false;

        // Flag to track if we have reached the peak
        boolean flag = false;
        int check = arr[0];  // Initialize the peak value to the first element

        // Flags to indicate if we've seen an increase or a decrease
        boolean dec = false;
        boolean inc = false;

        // Variable to track the previous value to check for duplicates
        int preValue = -1;
        for (int i = 0; i < arr.length; i++) {
            // Check for duplicate values
            if (preValue == arr[i]) {
                return false;  // If the current value equals the previous, return false
            } else {
                preValue = arr[i];  // Update the previous value
            }

            // Handle the increasing and decreasing phases of the mountain
            if (!flag) {
                // If we haven't reached the peak yet
                if (arr[i] > check) {
                    check = arr[i];  // Update peak value
                    inc = true;      // Mark that we've seen an increase
                } else if (arr[i] < check) {
                    // We have reached the peak
                    check = arr[i];  // Update peak value
                    flag = true;     // Set flag indicating we are now in the decreasing phase
                    // If we are at the last element, check if we have started decreasing
                    if (arr.length - 1 == i) {
                        dec = true;
                    }
                }
            } else {
                // We are in the decreasing phase
                if (arr[i] < check) {
                    check = arr[i];  // Continue to update peak value
                    dec = true;      // Confirm we are still decreasing
                } else {
                    return false;  // If we increase during the decreasing phase, return false
                }
            }
        }

        // We must have seen both an increase and a decrease to form a valid mountain
        if (!dec || !inc) {
            return false;
        }

        return true;  // Return true if it is a valid mountain array
    }
}

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.