longestplateau java code using single loop

Solutions on MaxInterview for longestplateau java code using single loop by the best coders in the world

showing results for - "longestplateau java code using single loop"
Michela
02 Sep 2016
1public static void printLargestPlateau(int[] values) {
2    int biggestStartIndex = -1;
3    int biggestLenth = 0;
4    int currentIndex = 1;
5    int currentPlateauStartIndex = 1;
6    int currentLenght = 1;
7    boolean plateauStarted = false;
8    while (currentIndex < values.length) {
9        if(isStartOfPlateau(currentIndex, values)){
10            plateauStarted = true;
11            currentPlateauStartIndex = currentIndex;
12        } else if (isEndOfPlateau(currentIndex, values)) {
13            if(plateauStarted && currentLenght > biggestLenth){
14                biggestLenth = currentLenght;
15                biggestStartIndex = currentPlateauStartIndex;
16            }
17            plateauStarted = false;
18            currentLenght = 1;
19        } else {
20            currentLenght++;
21        }
22        currentIndex++;
23    }
24    if(biggestStartIndex < 0){
25        System.out.println("No plateau");
26    } else {
27        System.out.println("Biggest plateau starts at index: "+biggestStartIndex +" and has length: "+biggestLenth);
28    }
29}
30
31private static boolean isStartOfPlateau(int index, int[] values){
32    if(index <= 0){
33        return false;
34    }
35    return values[index-1] < values[index];
36}
37
38private static boolean isEndOfPlateau(int index, int[] values){
39    if(index <= 0){
40        return false;
41    }
42    return values[index - 1] > values[index];
43}
44