Java Program to Find Peak Element of an Array using Binary Search

This is a Java Program to find peak element of an array. A peak element of an array is that element which is greater than its neighbors. The following program uses binary search approach to find a peak element. The time complexity of the following program is O (log n).

Here is the source code of the Java program to find peak element of an array using binary search. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java Program to Find the peak element of an array using 
  3.  * Binary Search approach
  4.  */
  5. import java.util.Scanner;
  6.  
  7. public class PeakElement2    
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.println("Enter size of array");
  13.         int N = scan.nextInt();
  14.         int[] arr = new int[ N ];
  15.         /* Accept N elements */
  16.         System.out.println("Enter "+ N +" elements");
  17.         for (int i = 0; i < N; i++)
  18.             arr[i] = scan.nextInt();
  19.         /* Find Peak Elements */        
  20.         System.out.print("\nPeak Elements : ");
  21.         peak(arr);
  22.         System.out.println();
  23.     } 
  24.     public static void peak(int[] arr)
  25.     {
  26.         peak(arr, 0, arr.length - 1);
  27.     }    
  28.     public static void peak (int arr[], int low, int high)
  29.     {
  30.         int N = arr.length;
  31.         if (high - low < 2)
  32.             return;
  33.         int mid = (low + high) / 2;
  34.         if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
  35.             System.out.print(arr[mid] +" ");
  36.         /* Recursively find other peak elements */        
  37.         peak (arr, low, mid);
  38.         peak (arr, mid, high);    
  39.     }        
  40. }

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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.