Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions PeakElementArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <vector>
using namespace std;

int peakElement(vector<int> &arr) {
int n = arr.size();

for(int i = 0; i < n; i++) {
bool left = true;
bool right = true;

// Check for element to the left
if(i > 0 && arr[i] <= arr[i - 1])
left = false;

// Check for element to the right
if(i < n - 1 && arr[i] <= arr[i + 1])
right = false;

// If arr[i] is greater than its left as well as
// its right element, return its index
if(left && right) {
return i;
}
}
return 0;
}

int main() {
vector<int> arr = {1, 2, 4, 5, 7, 8, 3};
cout << peakElement(arr);
return 0;
}