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

Queen's Attack II HackerRank Solution in Java with Explanation

Queen's Attack II Problem's Solution in Java (Chessboard Problem)   Problem Description : You will be given a square chess board with one queen and a number of obstacles placed on it. Determine how many squares the queen can attack.  A queen is standing on an n * n chessboard. The chess board's rows are numbered from 1 to n, going from bottom to top. Its columns are numbered from 1 to n, going from left to right. Each square is referenced by a tuple, (r, c), describing the row r and column c, where the square is located. The queen is standing at position (r_q, c_q). In a single move, queen can attack any square in any of the eight directions The queen can move: Horizontally (left, right) Vertically (up, down) Diagonally (four directions: up-left, up-right, down-left, down-right) The queen can move any number of squares in any of these directions, but it cannot move through obstacles. Input Format : n : The size of the chessboard ( n x n ). k : The number of obstacles...

Java Hashset HackerRank Solution | Programming Blog

Java Hashset HackerRank Solution with Explanation   Problem Statement :- In computer science, a set is an abstract data type that can store certain values, without any particular order, and no repeated values. {1,2,3} is an example of a set, but {1,2,2} is not a set. Today you will learn how to use sets in java by solving this problem. You are given n pairs of strings. Two pairs (a,b) and (c,d) are identical if a = c and b = d. That also implies (a,b) is not same as (b,a). After taking each pair as input, you need to print number of unique pairs you currently have. See full problem description in HackerRank Website :- https://www.hackerrank.com/challenges/java-hashset/problem Let's see solution of problem. import java.util.HashSet; import java.util.Scanner; public class Solution {     public static void main(String[] args) {         Scanner s = new Scanner(System.in);         System.out.println("Enter tot...