# Mountain

<figure><img src="https://3321732264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlxxyP2YZ5BoKtSlOBx96%2Fuploads%2F8eAvpx124JmuL5BxYLUe%2Fimage.png?alt=media&#x26;token=e5a5e7e2-2eae-439e-867a-6522916f8fa1" alt=""><figcaption></figcaption></figure>

An array of positive integer values has the mountain property if the elements are ordered such that successive values increase until a maximum value (the peak of the mountain) is reached and then the successive values decrease. The Mountain class declaration shown below contains methods that can be used to determine if an array has the mountain property.&#x20;

You will implement three methods in the Mountain class. For each method, you and your partner will be  **DRIVER**(writing the code) and **NAVIGATOR**(Offering feedback).&#x20;

```java
public class Mountain {
    /*
    @param array: an array of positive integer values
    @param stop: the last index to check
    
        Precondition: 0 <= stop < array.length
    
    @return true if for each j such that 0<=j<stop, array[j] < array[j+1]; 
    false otherwise.
    */
    
    public static boolean isIncreasing(int[] array, int stop) {
        for(int i=0; i<stop; i++) {
          /*if previous value is no less than the latter,
          return false since it doesn't increase continuously.  
          */
            if(array[i] >= array[i+1]) return false; 
        }
        return true;
    }
    
    /*
    @param array: an array of positive integer values
    @param start: the first index to check
        
        Precondition: 0 <= start < array.length-1
    
    @return true if for each j such that start<=j<array.length-1, array[j] > array[j+1]; 
    false otherwise.
    */
    
    public static boolean isDecreasing(int[] array, int start) {
        return false;
    }

    /*
    @param array: an array of positive values
        
        Precondition: array.length > 0
    
    @return the index of the first peak(local maximum) in the array, if it exists; -1 otherwise.
    
    */
    public static int getPeakIndex(int[] array) {
        return -1;
    }
    
    /*
    @param array: an array of positive integer values
    
        Precondition: array.length > 0
        
    @return true if array contains values ordered as a mountain; false otherwise.
    
    */
    public static boolean isMountain(int[] array) {
        return false;
    }
    
}
```

### **Part(1)**&#x20;

*<mark style="background-color:orange;">**Please decide whom to be the DRIVER or NAVIGATOR.**</mark>*&#x20;

Complete method ***isDecreasing(int\[] array, int start)***. The method return true if the mountain decreases continuously from ***start.***&#x20;

For example,&#x20;

| Parameters                              | isDecreasing(arr, start)                                        |
| --------------------------------------- | --------------------------------------------------------------- |
| <p>arr: {1, 2, 3, 2, 1}<br>start: 0</p> | return false, since {1,2,3,2,1} is not decreasing continuously. |
| <p>arr: {1, 2, 3, 2, 1}<br>start: 2</p> | return true, since {3,2,1} is decreasing continuously.          |
| <p>arr: {1, 2, 3, 3, 1}<br>start: 2</p> | return false, since {3,3,1} is not decreasing continuously.     |

Mathematicall&#x79;***,*****&#x20;isDecreasing()** returns **true** if:&#x20;

*for each j such that start<=j\<array.length-1,  array\[j] > array\[j+1];*&#x20;

Otherwise return false.

Method **IsIncreasing(int\[] array, int stop)** has been provided for you, which returns true if the mountain increases continuously until the stop. You can refer to it as a hint.

### **Part(2)**

*<mark style="background-color:orange;">**Please change roles now.**</mark>*&#x20;

Write the Mountain method **getPeakIndex**. Method **getPeakIndex** returns the index of the first peak found in the parameter array, if one exists.&#x20;

A peak is defined as an element whose value is greater than the value of the element immediately before it and is also greater than the value of the element immediately after it.&#x20;

Method **getPeakIndex** starts at the beginning of the array and returns the index of the first peak that is found or -1 if no peak is found.

For example, the following table illustrates the results of several calls to **getPeakIndex**.

<figure><img src="https://3321732264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlxxyP2YZ5BoKtSlOBx96%2Fuploads%2F8D1NVaSH17GdxrpGkFy2%2Fimage.png?alt=media&#x26;token=64943af8-3b86-4f43-b64e-04f559b4991e" alt=""><figcaption></figcaption></figure>

### **Part(3)**

*<mark style="background-color:orange;">**Please change roles now.**</mark>*&#x20;

Write the Mountain method **isMountain**. Method **isMountain** returns true if the values in the parameter array are ordered as a mountain; otherwise, it returns false.&#x20;

The values in array are ordered as a mountain if **all three** of the following conditions hold.

* There must be a peak.
* The array elements with an index smaller than the peak’s index must appear in increasing order.
* The array elements with an index larger than the peak’s index must appear in decreasing order.

For example, the following table illustrates the results of several calls to isMountain.

<figure><img src="https://3321732264-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FlxxyP2YZ5BoKtSlOBx96%2Fuploads%2FfEZVCt2qfttlXqEFZRZ7%2Fimage.png?alt=media&#x26;token=2d94071c-b626-40f8-8991-8e273d9e34f1" alt=""><figcaption></figcaption></figure>

**You have to call&#x20;*****`getPeakIndex()`*****,&#x20;*****`isIncreasing()`*****and&#x20;*****`isDecreasing()`*****&#x20;in Part(3) to receive full credit.**&#x20;

### TEST

Test class has been provided below:&#x20;

```java
public class Main
{
	public static void main(String[] args) {
		int[] arr1 = {11,22,33,22,11};
		int[] arr2 = {11,22,11,22,11};
		int[] arr3 = {11,22,33,55,77};
		int[] arr4 = {99,33,55,77,120};
		int[] arr5 = {99,33,55,77,55};
		int[] arr6 = {33,22,11};
		
		System.out.println(Mountain.getPeakIndex(arr1)); //2
		System.out.println(Mountain.getPeakIndex(arr2)); //1
		System.out.println(Mountain.getPeakIndex(arr3)); //-1
		System.out.println(Mountain.getPeakIndex(arr4)); //-1
		System.out.println(Mountain.getPeakIndex(arr5)); //3
		System.out.println(Mountain.getPeakIndex(arr6)); //-1
		
		int[] m1 = {1,2,3,2,1};
		int[] m2 = {1,2,1,2,1};
		int[] m3 = {1,2,3,1,5};
		int[] m4 = {1,4,2,1,0};
		int[] m5 = {9,3,5,7,5};
		int[] m6 = {3,2,1};
		
		System.out.println(Mountain.isMountain(m1)); //true
        	System.out.println(Mountain.isMountain(m2)); //false
		System.out.println(Mountain.isMountain(m3)); //false
		System.out.println(Mountain.isMountain(m4)); //true
		System.out.println(Mountain.isMountain(m5)); //false
		System.out.println(Mountain.isMountain(m6)); //false

	}
}
```

The result should be like:

```
2
1
-1
-1
3
-1
true
false
false
true
false
false
```
