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...

Simple Text Editor HackerRank Solution in Java with Explanation

Simple Text Editor HackerRank Solution in Java with Explanation Problem Description : Implement a simple text editor. The editor initially contains an empty string S. Perform Q operations of the following 4 types: append (W) - Append string W to the end of S. delete (k) - Delete the last K characters of S. print (k) - Print the kth character of S. undo () - Undo the last (not previously undone) operation of type 1 or 2, reverting S to the state it was in prior to that operation. Example : Input : S = "abcde" Operations = ["1 fg", "3 6", "2 5", "4", "3 7", "4", "3 4"] index   S          ops[index]    explanation -----   ------     ----------    ----------- 0       abcde      1 fg          append fg 1       abcdefg    3 6        ...