
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find the peak element of an array using Binary Search approach
In this C++ program, we find out one of the peaks in the array can be found Using binary search approach. This algorithm returns the first peak found as a result with time complexity of the algorithm is O(log(n)).
Algorithm
Begin PeakElement() function has ‘arr’ the array of data, start and end index in the argument list. Assign the mid of subpart of the array. If mid is at the boundary index and value at mid is higher than its neighbor then return mid as peak. If the value at mid is greater than both of its neighbors then return mid as peak. If the value at the right of mid is greater than mid then send second sub-part of the array into PeakElement() as argument. If the value at the left of mid is greater than mid then send first sub-part of the array into PeakElement() as argument. End
Example Code
#include<iostream> using namespace std; int PeakElement(int a[], int start, int end) { int i, mid; mid = (end+start+1)/2; if((a[mid] > a[mid+1] && mid == start)||(a[mid] > a[mid-1] && mid == end)) { return a[mid]; } else if(a[mid] < a[mid-1] && a[mid] > a[mid+1]) { return a[mid]; } else if(a[mid] <= a[mid+1]) { return PeakElement(a, mid+1, end); } else if(a[mid] <= a[mid-1]) { return PeakElement(a, start,mid-1); } } int main() { int n, i, p; cout<<"\nEnter the number of data element: "; cin>>n; int arr[n]; for(i = 0; i < n; i++) { cout<<"Enter element "<<i+1<<": "; cin>>arr[i]; } p = PeakElement(arr, 0, n-1); cout<<"\nThe peak element of the given array is: "<<p; return 0; }
Output
Enter the number of data element: 5 Enter element 1: 45 Enter element 2: 26 Enter element 3: 70 Enter element 4: 60 Enter element 5: 15 The peak element of the given array is: 70
- Related Questions & Answers
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ Program to Find Maximum Element in an Array using Binary Search
- C++ Program to Find the maximum subarray sum using Binary Search approach
- C++ Program to find the median of two sorted arrays using binary search approach
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- C program to search an array element using Pointers.
- How to find minimum element in an array using binary search in C language?
- C++ Program to Find Minimum Element in an Array using Linear Search
- C++ Program to Search for an Element in a Binary Search Tree
- Find a peak element in C++
- Find a peak element in a 2D array in C++
- Search in an array with Binary search using JavaScript
- C++ Program to Find Largest Element of an Array
- C++ Program to Find the Minimum value of Binary Search Tree
- Peak Element in 2D array
Advertisements