Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Oct 27, 2024. It is now read-only.

Commit ec485a5

Browse files
Merge pull request #19 from aliat110/add_find_peak_element_cpp
Add FindPeakElement for C++
2 parents 9dd98c3 + f346c62 commit ec485a5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎C++/FindPeakElement.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int findPeakElement(int arr[], int n)
5+
{
6+
int start = 0;
7+
int end = n - 1;
8+
while (start < end)
9+
{
10+
int mid = start + (end - start) / 2;
11+
if (arr[mid] > arr[mid + 1])
12+
{
13+
end = mid;
14+
}
15+
else
16+
{
17+
start = mid + 1;
18+
}
19+
}
20+
return start;
21+
}
22+
23+
int main()
24+
{
25+
int arr[] = {1000, 11, 445, 1, 330, 3000};
26+
int n = sizeof(arr) / sizeof(arr[0]);
27+
int ans = findPeakElement(arr, n);
28+
cout << "Index of the peak element of array [ 1000, 11, 445, 1, 330, 3000 ] is " << ans;
29+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /