Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

852. Peak Index in a Mountain Array

Medium68.0% Acceptance
ArrayBinary Search
Asked by:
A
Amazon
Microsoft
ProblemSolutions (12)VideosCompanies (23)Notes

Problem Statement

You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease.

Return the index of the peak element.

Your task is to solve it in O(log(n)) time complexity.

Example 1:

Input: arr = [0,1,0]

Output: 1

Example 2:

Input: arr = [0,2,1,0]

Output: 1

Example 3:

Input: arr = [0,10,5,2]

Output: 1

Constraints:

  • 3 <= arr.length <= 105
  • 0 <= arr[i] <= 106
  • arr is guaranteed to be a mountain array.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
M
W
Walmart
F
Facebook
W
Wipro
+18

Approach

In #852 Peak Index in a Mountain Array, the array follows a special structure: values strictly increase up to a peak and then strictly decrease. The goal is to find the index of this peak element efficiently.

A straightforward idea is a linear scan, checking where the sequence changes from increasing to decreasing. While simple, this takes O(n) time.

A more optimal approach leverages Binary Search. Since the array forms a mountain shape, we can compare arr[mid] with arr[mid + 1]. If the next value is larger, the peak lies on the right; otherwise, it lies on the left (including mid). Repeating this process gradually narrows the search space until the peak index is found.

This technique uses the sorted-like structure of the mountain slope, reducing the search range each step and achieving logarithmic time complexity with constant extra space.

Complexity

ApproachTime ComplexitySpace Complexity
Linear ScanO(n)O(1)
Binary Search (Optimal)O(log n)O(1)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (12)

Binary Search Approach

This approach leverages the characteristics of a mountain array to achieve an O(log n) time complexity using binary search. The concept is to look for the part of the array where the increase turns into a decrease. We will perform a binary search for the peak index by comparing middle elements and deciding the side that can have the peak element.

Time complexity: O(log n) since we are performing a binary search.
Space complexity: O(1) as we are using a constant amount of extra space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int peakIndexInMountainArray(int* arr, int arrSize){
4    int low = 0, high = arrSize - 1;

Explanation

In this solution, we apply a binary search on the mountain array. The loop continues until low equals high. Each time, we find the mid of the current subarray. If the middle element is less than the next one, it implies the peak is on the right, so we set low to mid + 1. Otherwise, the peak must be at mid or to the left of it, so high is set to mid. The low eventually points to the peak index.

Linear Scan Approach

This approach involves a simple linear scan to find the peak element. Although not achieving the necessary O(log n) time complexity, it serves as a straightforward solution to verify correctness.

Time complexity: O(n) as each element is visited once.
Space complexity: O(1).

CC++JavaPythonC#JavaScript
1#include

Video Solutions

Watch expert explanations and walkthroughs

Find Peak Element - Leetcode 162 - Python

NeetCodeIO
11:0263,861 views

Asked By Companies

23 companies
A
Amazon
M
Microsoft
W
Walmart
F
Facebook
W
Wipro
B
BYJUS
U
Uber
D
Dunzo
t
tcs
H
Hike
O
Ola
G
Goldman Sachs
B
Bloomberg
A
APT Portfolio
D
DE Shaw India
G
Groupon
J
Josh Technologies
Z
Zeta
A
Accolite
A
Apple
P
Proschant India
A
Adobe
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Median of Two Sorted ArraysHard
Search in Rotated Sorted ArrayMedium
Find First and Last Position of Element in Sorted ArrayMedium
Search Insert PositionEasy
More similar problems

Related Topics

ArrayBinary Search

Problem Stats

Acceptance Rate68.0%
DifficultyMedium
Companies23

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does binary search work for a mountain array?

Binary search works because the array has a predictable structure: strictly increasing then strictly decreasing. This property allows you to determine the direction of the peak by comparing adjacent values and narrowing the search space.

Is Peak Index in a Mountain Array asked in FAANG interviews?

Yes, variations of this problem are common in technical interviews, including FAANG-style interviews. It tests understanding of binary search patterns and the ability to exploit structured arrays.

What data structure is used for Peak Index in a Mountain Array?

The problem primarily uses an array. The key idea is not a special data structure but leveraging the mountain array property with binary search to efficiently locate the peak.

What is the optimal approach for Peak Index in a Mountain Array?

The optimal approach uses binary search. By comparing the middle element with its next element, you can determine which side of the mountain the peak lies on and shrink the search range until the peak index is found.

5
while
(
low
<
high
)
{
6
int
mid
=
low
+
(
high
-
low
)
/
2
;
7
if
(
arr
[
mid
]
<
arr
[
mid
+
1
]
)
{
8
low
=
mid
+
1
;
9
}
else
{
10
high
=
mid
;
11
}
12
}
13
return
low
;
14
}
15
16
int
main
(
)
{
17
int
arr
[
]
=
{
0
,
2
,
1
,
0
}
;
18
int
n
=
sizeof
(
arr
)
/
sizeof
(
arr
[
0
]
)
;
19
printf
(
"Peak Index: %d\n"
,
peakIndexInMountainArray
(
arr
,
n
)
)
;
20
return
0
;
21
}
<stdio.h>
2
3
int
peakIndexInMountainArray
(
int
*
arr
,
int
arrSize
)
{
4
for
(
int
i
=
1
;
i
<
arrSize
-
1
;
++
i
)
{
5
if
(
arr
[
i
]
>
arr
[
i
-
1
]
&&
arr
[
i
]
>
arr
[
i
+
1
]
)
{
6
return
i
;
7
}
8
}
9
return
-
1
;
// Included as a safety net, though problem constraints ensure a peak exists.
10
}
11
12
int
main
(
)
{
13
int
arr
[
]
=
{
0
,
2
,
1
,
0
}
;
14
int
n
=
sizeof
(
arr
)
/
sizeof
(
arr
[
0
]
)
;
15
printf
(
"Peak Index: %d\n"
,
peakIndexInMountainArray
(
arr
,
n
)
)
;
16
return
0
;
17
}

Explanation

The C solution performs a linear scan from index 1 to arrSize - 2 (exclusive). For each element, it checks if it is larger than its neighbors. If true, the index is returned as the peak index.