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
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
add you code file to this folder
#include <iostream>
#include <vector>
using namespace std;

int findPeak(std::vector<int>& nums) {
int left = 0;
int right = nums.size() - 1;
int findPeakElement(int arr[], int n) {
int low = 0;
int high = n - 1;

while (left < right) {
int mid = left + (right - left) / 2;
while (low <= high) {
int mid = low + (high - low) / 2;

if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
// Check if mid is a peak
if ((mid == 0 || arr[mid] >= arr[mid - 1]) && (mid == n - 1 || arr[mid] >= arr[mid + 1])) {
return arr[mid];
}

// If the element to the right of mid is greater, move to the right
if (mid < n - 1 && arr[mid] < arr[mid + 1]) {
low = mid + 1;
}
// Otherwise, move to the left
else {
high = mid - 1;
}
}

return left;
// If no peak is found, return -1 or handle it as needed
return -1;
}

int main() {
std::vector<int> nums = {1, 3, 20, 4, 1, 0};
int arr[] = {1, 3, 20, 4, 1, 0};
int n = sizeof(arr) / sizeof(arr[0]);

int peakIndex = findPeak(nums);
int peak = findPeakElement(arr, n);

std::cout << "Peak element is: " << nums[peakIndex] << " at index " << peakIndex << std::endl;
if (peak != -1) {
cout << "Peak element is: " << peak << endl;
} else {
cout << "No peak element found in the array." << endl;
}

return 0;
}