Skip to content

Commit d034d75

Browse files
authored
Merge pull request #3952 from Ishitamukherjee2004/new-branch
Solution of Peak Element from gfg is added
2 parents 1291d50 + 1c1650b commit d034d75

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
id: peak-element
3+
title: Peak Element
4+
sidebar_label: Peak-Element
5+
tags:
6+
- Modular Arithmetic
7+
- Algorithms
8+
description: "This tutorial covers the solution to the Peak Element problem from the GeeksforGeeks."
9+
---
10+
## Problem Description
11+
Given an `0`-indexed array of integers `arr[]` of size n, find its peak element and return it's index. An element is considered to be peak if it's value is greater than or equal to the values of its adjacent elements (if they exist).
12+
13+
Note: The output will be `1` if the index returned by your function is correct; otherwise, it will be `0`.
14+
15+
## Examples
16+
17+
**Example 1:**
18+
19+
```
20+
Input: n = 3, arr[] = {1, 2, 3}
21+
Output: 1
22+
Explanation: If the index returned is 2, then the output printed will be 1. Since arr[2] = 3 is greater than its adjacent elements, and there is no element after it, we can consider it as a peak element. No other index satisfies the same property, so answer will be printed as 0.
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: n = 7, arr[] = {1, 1, 1, 2, 1, 1, 1}
29+
Output: 1
30+
Explanation: In this case there are 5 peak elements with indices as {0,1,3,5,6}. Returning any of them will give you correct answer.
31+
```
32+
33+
34+
Expected Time Complexity: O(logn)
35+
36+
Expected Auxiliary Space: O(1)
37+
38+
## Constraints
39+
40+
* `1 ≤ N ≤ 10^5`
41+
42+
## Problem Explanation
43+
44+
The task is to traverse the array and find the peak element.
45+
46+
## Code Implementation
47+
48+
### C++ Solution
49+
50+
```cpp
51+
#include <iostream>
52+
#include <vector>
53+
54+
int findPeakElement(const std::vector<int>& arr) {
55+
int n = arr.size();
56+
if (n == 1) {
57+
return 0;
58+
}
59+
if (arr[0] >= arr[1]) {
60+
return 0;
61+
}
62+
if (arr[n - 1] >= arr[n - 2]) {
63+
return n - 1;
64+
}
65+
for (int i = 1; i < n - 1; i++) {
66+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1]) {
67+
return i;
68+
}
69+
}
70+
return -1; // return -1 if no peak element is found
71+
}
72+
int main() {
73+
std::vector<int> arr = {1, 3, 20, 4, 1, 0};
74+
int peakIndex = findPeakElement(arr);
75+
if (peakIndex != -1) {
76+
std::cout << "Peak element is at index " << peakIndex << std::endl;
77+
} else {
78+
std::cout << "No peak element found" << std::endl;
79+
}
80+
return 0;
81+
}
82+
83+
84+
```
85+
86+
```java
87+
import java.util.*;
88+
89+
public class Main {
90+
public static int findPeakElement(int[] arr) {
91+
int n = arr.length;
92+
if (n == 1) {
93+
return 0;
94+
}
95+
if (arr[0] >= arr[1]) {
96+
return 0;
97+
}
98+
if (arr[n - 1] >= arr[n - 2]) {
99+
return n - 1;
100+
}
101+
for (int i = 1; i < n - 1; i++) {
102+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1]) {
103+
return i;
104+
}
105+
}
106+
return -1; // return -1 if no peak element is found
107+
}
108+
public static void main(String[] args) {
109+
int[] arr = {1, 3, 20, 4, 1, 0};
110+
int peakIndex = findPeakElement(arr);
111+
if (peakIndex != -1) {
112+
System.out.println("Peak element is at index " + peakIndex);
113+
} else {
114+
System.out.println("No peak element found");
115+
}
116+
}
117+
}
118+
119+
120+
```
121+
122+
```python
123+
124+
def find_peak_element(arr):
125+
n = len(arr)
126+
if n == 1:
127+
return 0
128+
if arr[0] >= arr[1]:
129+
return 0
130+
if arr[n - 1] >= arr[n - 2]:
131+
return n - 1
132+
for i in range(1, n - 1):
133+
if arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]:
134+
return i
135+
return -1 # return -1 if no peak element is found
136+
137+
arr = [1, 3, 20, 4, 1, 0]
138+
peak_index = find_peak_element(arr)
139+
if peak_index != -1:
140+
print("Peak element is at index", peak_index)
141+
else:
142+
print("No peak element found")
143+
144+
145+
```
146+
147+
```javascript
148+
function findPeakElement(arr) {
149+
let n = arr.length;
150+
if (n === 1) {
151+
return 0;
152+
}
153+
if (arr[0] >= arr[1]) {
154+
return 0;
155+
}
156+
if (arr[n - 1] >= arr[n - 2]) {
157+
return n - 1;
158+
}
159+
for (let i = 1; i < n - 1; i++) {
160+
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1]) {
161+
return i;
162+
}
163+
}
164+
return -1; // return -1 if no peak element is found
165+
}
166+
167+
let arr = [1, 3, 20, 4, 1, 0];
168+
let peakIndex = findPeakElement(arr);
169+
if (peakIndex !== -1) {
170+
console.log(`Peak element is at index ${peakIndex}`);
171+
} else {
172+
console.log("No peak element found");
173+
}
174+
175+
```
176+
177+
## Solution Logic:
178+
This solution iterates through the array and checks each element to see if it's greater than or equal to its adjacent elements. If it finds such an element, it returns its index. If no such element is found, it returns -1.
179+
180+
181+
182+
183+
## Time Complexity
184+
185+
* The time complexity is $O(n)$ where n is the input number.
186+
187+
188+
## Space Complexity
189+
190+
* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array.

0 commit comments

Comments
 (0)