11 Dec 2017

write a program in java to Find the maximum element in an array which is first increasing and then decreasing.means Find the peak element of array after which elements are decreasing.


Input: arr[] = {8, 10, 20, 80, 100, 200, 400, 500, 3, 2, 1}
Output: 500

Input: arr[] = {1, 3, 50, 10, 9, 7, 6}
Output: 50 Corner case (No decreasing part)

Input: arr[] = {10, 20, 30, 40, 50}
Output: 50 Corner case (No increasing part)

Input: arr[] = {120, 100, 80, 20, 0}

Output: 120



class FindPeakElement {
          // function to find the
          // maximum element
          static int findMaximum(int arr[], int low, int high) {
                   int max = arr[low];
                   int i;
                   for (i = low; i <= high; i++) {
                             if (arr[i] > max)
                                      max = arr[i];
                   }
                   return max;
          }

          // main function
          public static void main(String[] args) {
                   int arr[] = { 1, 30, 40, 50, 60, 70, 23, 20 };
                   int n = arr.length;
                   System.out.println("The maximum element is " +
               findMaximum(arr, 0, n - 1));
          }
}

Output: