Monday, 27 November 2017

Java Program to find the peak element of an array using Binary Search approach


Code:

import java.util.Scanner;

public class PeakElement2    
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter size of array");
        int N = scan.nextInt();
        int[] arr = new int[ N ];
        /* Accept N elements */
        System.out.println("Enter "+ N +" elements");
        for (int i = 0; i < N; i++)
            arr[i] = scan.nextInt();
        /* Find Peak Elements */        
        System.out.print("\nPeak Elements : ");
        peak(arr);
        System.out.println();
    } 
    public static void peak(int[] arr)
    {
        peak(arr, 0, arr.length - 1);
    }    
    public static void peak (int arr[], int low, int high)
    {
        int N = arr.length;
        if (high - low < 2)
            return;
        int mid = (low + high) / 2;
        if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
            System.out.print(arr[mid] +" ");
        /* Recursively find other peak elements */        
        peak (arr, low, mid);
        peak (arr, mid, high);    
    }        
}


Output:

Enter size of array
8
Enter 8 elements
87 63 51 25 40 24 6 1

Peak Elements : 40



More Java Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...