Skip to main content

Valid Mountain Array Leetcode solution in Java | Programming Tutorial

Valid Mountain Array Java Solution with Explanation

Valid Mountain Array java solution

Problem Description :-

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

See full description on LeetCode website :-

Lets see example of input and its output :-

Example 1 :-

Input : arr = [1, 2, 3, 4, 3];
Output : true

Example 2 :-

Input : arr = [1, 2, 3, 4, 5, 6];
Output : false

Example 3 :-

Input : arr = [0, 2, 3, 4, 5, 6, 5, 5, 4, 3];
Output : false

Example 4 :-

Input : arr = [0, 3, 2, 1];
Output : true

Lest go for solution :-

Solution 1

class Solution {
    
    public boolean validMountainArray(int[] array) {
        int up = 0;
        int down = 0;

        if (array.length >= 3) {
            
            for (int i=0; i<array.length-1; i++) {
                if (array[i] == array[i+1]){
                    return false;
                }


                if (down == 0 && array[i+1] > array[i]) {
                    up = 1;
                } else if (array[i+1] < array[i]){
                    down = 1;
                } else {
                    return false;
                }
            }
            
            if (down == 1 && up ==1) {
                return true;
            }
        }
        return false;
    }
}

Solution Explanation :-

  • Declare two int variable up and down and initialize with 0.
  • First condition is check if array length is greater and equals to 3 or not. (As given in problem description).
  • If array length is greater than 3, then loop through array length.
  • Return false if current and next array value is same (As per problem description).
  • If both value are not same so first we are checking down is 0 and next array value is greater than current value or not. based on that we are checking it is going upward. If condition becomes true then assign 1 value to up. Using this if condition we can identify that our array value is going up (Valid Mountain).
  • In else if we are checking that it is going down correctly or not.
  • If both condition not satisfied then return false in else condition.
  • At last, we check if both up and down are true or not. If both are true then return true. otherwise return false after for loop.

Output Explanation :-

array = [1, 2, 3, 4, 3]

  •  if (array.length > 3) becomes true.
    • Loop through array. | i = 0
      • array[0] == array[1] | 1 == 2 becomes false.
      • down == 0 && array[i+1] > array[i] | 2 > 1 becomes true.
        • up = 1.
    •  i = 1
      • array[1] == array[2] | 2 == 3 becomes false.
      • down == 0 && array[i+1] > array[i] | 3 > 2 becomes true.
    • i = 2
      • array[2] == array[3] | 3 == 4 becomes false.
      • down == 0 && array[i+1] > array[i] | 4 > 3 becomes true.
    • i = 3
      • array[3] == array[4] | 3 == 4 becomes false.
      • down == 0 && array[i+1] > array[i] | 3 > 4 becomes false.

      • array[i+1] < array[i] | 3 < 4 becomes true.
      • down = 1
    • Loop ends.
  •  down == 1 && up ==1 becomes true. So function return true and we got answer.

 

Solution 2

public class Solution {
    
    public static boolean validMountainArray(int[] array) {
        int arrayLength = array.length;
        int i = 0;

        // walk up
        while (i+1 < arrayLength && array[i] < array[i+1]) {
            i++;
        }
       
        // peak can't be first or last
        if (i == 0 || i == arrayLength-1) {
            return false;
        }
       
        // walk down
        while (i+1 < arrayLength && array[i] > array[i+1]) {
            i++;
        }
       
        return i == arrayLength-1;
    }
}

Explanation :-

  • Get array length in variable. Declare and initialize i with 0.
  • In first while loop, we are going upward direction. So we are checking that arrayLength is greater than i+1 and current array value is less than next array value. If it is going upward then we are incrementing i by 1.
  • In if condition, We are checking that it is staring as upward direction or not. If i is 0 then it is definitely not going upward.
  • In second while loop, we are going downward direction. In that current array value is must have greater than next array value. 
  • At last, we are checking i with arrayLength -1, if it is same then it returns true otherwise returns false.


Happy Coding.

Other articles you may like :-

 

Comments

Popular posts from this blog

Plus Minus HackerRank Solution in Java | Programming Blog

Java Solution for HackerRank Plus Minus Problem Given an array of integers, calculate the ratios of its elements that are positive , negative , and zero . Print the decimal value of each fraction on a new line with 6 places after the decimal. Example 1 : array = [1, 1, 0, -1, -1] There are N = 5 elements, two positive, two negative and one zero. Their ratios are 2/5 = 0.400000, 2/5 = 0.400000 and 1/5 = 0.200000. Results are printed as:  0.400000 0.400000 0.200000 proportion of positive values proportion of negative values proportion of zeros Example 2 : array = [-4, 3, -9, 0, 4, 1]  There are 3 positive numbers, 2 negative numbers, and 1 zero in array. Following is answer : 3/6 = 0.500000 2/6 = 0.333333 1/6 = 0.166667 Lets see solution Solution 1 import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util.stream.*; import static java.util.st

Flipping the Matrix HackerRank Solution in Java with Explanation

Java Solution for Flipping the Matrix | Find Highest Sum of Upper-Left Quadrant of Matrix Problem Description : Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix. Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix's upper-left quadrant is maximal.  Input : matrix = [[1, 2], [3, 4]] Output : 4 Input : matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]] Output : 119 + 114 + 56 + 125 = 414 Full Problem Description : Flipping the Matrix Problem Description   Here we can find solution using following pattern, So simply we have to find Max of same number of box like (1,1,1,1). And last