Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class Mountain {
- public static boolean isIncreasing(int[] array, int stop) {
- for (int i = 0; i < stop; i++)
- if (array[i] > array[i + 1])
- return false;
- return true;
- }
- public static boolean isDecreasing(int[] array, int start) {
- for (int j = start; j<array.length - 1; j++)
- if (array[j] < array[j+1])
- return false;
- return true;
- }
- public static int getPeakIndex(int[] array) {
- for (int i = 1; i < array.length - 1; i++)
- if (array[i] > array[i - 1] && array[i] > array[i + 1])
- return i;
- return -1;
- }
- public static boolean isMountain(int[] array) {
- int index = getPeakIndex(array);
- return index != -1 && isIncreasing(array, index - 1) && isDecreasing(array, index + 1);
- }
- }
- public class MountainRunner {
- public static void main(String[] args) {
- String output = "";
- int[][] arr = {{1, 2, 3, 2, 1}, {1, 2, 1, 2, 1}, {1, 2, 3, 1, 5}, {1, 4, 2, 1, 0}, {9, 3, 5, 7, 5}, {3, 2, 1}};
- for (int[] a : arr)
- System.out.println((Mountain.getPeakIndex(a) != -1 ? "Peak is " + Mountain.getPeakIndex(a) + " - " : "No peak - ")
- + Arrays.toString(a) + " is a mountain :: " + Mountain.isMountain(a));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement