Valid Mountain Array Java Solution with Explanation
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 : trueExample 2 :-
Input : arr = [1, 2, 3, 4, 5, 6];
Output : falseExample 3 :-
Input : arr = [0, 2, 3, 4, 5, 6, 5, 5, 4, 3];
Output : falseExample 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 :-
- Sort Java List using Comparator Interface for Three Object Values | Hackerrank Solution
- Merge Two Sorted Arrays in Java with Explanation | LeetCode Solution
Comments
Post a Comment