# Java Program to Find Local Maximas in an Array – Sanfoundry

This is the Java Program to Find Local Maximas in an Array.

Problem Description

Given an array of integers, find out the local maxima present in the array.
An element in an array is a local maxima if it greater than the element after it, and the element before it.
For the elements at the extreme end only one check is required, that is, the element following the first element or the element before the last element.
In case of two or more maxima, only one of them is returned.

Example:
Array = [1, 2, 3, 4, 5, -1]

Output:
5

Problem Solution

The idea here is to use an algorithm, similar to the binary search. Check the middle element of the array, if it is greater than the elements following it and the element preceding it, then it is the local maxima, else if it is greater than the preceding element, then the local maxima is in the left half, else the local maxima is in the right half.

Program/Source Code

Here is the source code of the Java Program to Find Local Maximas in an Array. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

1. ` `
2. `//Java Program to Find Local Maximas in an Array`
3. ` `
4. `import java.io.BufferedReader;`
5. `import java.io.InputStreamReader;`
6. ` `
7. `public class LocalMaxima {`
8. `    // Function to return the index of the local Maxima`
9. `    static int localMaxima(int[] array){`
10. `        int low,mid,high;`
11. `        low = 0;`
12. `        high = array.length-1;`
13. `        int ans;`
14. `        while(low<=high){`
15. `            mid = (low + high)/2;`
16. `            if((mid == 0 || array[mid-1] < array[mid]) `
17. `                && (mid == array.length-1 || array[mid+1] < array[mid])){`
18. `                return mid;`
19. `            }`
20. `            else if(mid > 0 && array[mid-1] > array[mid]){`
21. `                high = mid-1;`
22. `            }`
23. `            else{`
24. `                low = mid+1;`
25. `            }`
26. `        }`
27. `        return -1;`
28. `    }`
29. ` `
30. `    // Function to read input`
31. `    public static void main(String[] args) {`
32. `        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));`
33. `        int size;`
34. `        System.out.println("Enter the size of the array");`
35. `        try {`
36. `            size = Integer.parseInt(br.readLine());`
37. `        } catch (Exception e) {`
38. `            System.out.println("Invalid Input");`
39. `            return;`
40. `        }`
41. `        int[] array = new int[size];`
42. `        System.out.println("Enter array elements");`
43. `        int i;`
44. `        for (i = 0; i < array.length; i++) {`
45. `            try {`
46. `                array[i] = Integer.parseInt(br.readLine());`
47. `            } catch (Exception e) {`
48. `                System.out.println("An error occurred");`
49. `                return;`
50. `            }`
51. `        }`
52. `        int index = localMaxima(array);`
53. `        System.out.println("The local maxima is " + array[index]);`
54. `    }`
55. `}`

Program Explanation

1. In the function localMaxima(), we initialize two variables high and low as array.length-1 and 0 respectively.
2. Using a loop while(low <=high), we first calculate the middle index.
3. Now, in the condition if((mid == 0 || array[mid-1] < array[mid]) && (mid == array.length-1 || array[mid+1] < array[mid])), we check whether the element at current middle index is a maxima.
4. If the element is a maxima, then we return the current middle index, otherwise using the condition else if(mid > 0 && array[mid-1] > array[mid]), we first check that we are not at the beginning of the array and finally, check if the preceding element is greater than the current middle element.
5. If it is, we then set high to mid-1, to look in the first half of the array. Otherwise, we set low to mid+1 to look for the maxima in the second half of the array.

Time Complexity: O(log(n)) where n is the number of elements in the array.

Runtime Test Cases

```
Case 1 (Positive test case - local maxima is not at the extreme ends):

Enter the size of the array
6
Enter array elements
1
2
3
4
5
-1
The local maxima is 5

Case 2 (Positive test case - local maxima is at the beginning of the array):

Enter the size of the array
8
Enter array elements
8
7
6
5
4
3
2
1
The local maxima is 8

Case 3 (Positive test case - local maxima is at the end of the array):

Enter the size of the array
6
Enter array elements
1
2
3
4
5
6
The local maxima is 6```

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!