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

845. Longest Mountain in Array

Medium40.7% Acceptance
ArrayTwo PointersDynamic Programming
Asked by:
Goldman Sachs
ProblemSolutions (12)VideosCompanies (4)Notes

Problem Statement

You may recall that an array arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Given an integer array arr, return the length of the longest subarray, which is a mountain. Return 0 if there is no mountain subarray.

Example 1:

Input: arr = [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.

Example 2:

Input: arr = [2,2,2]
Output: 0
Explanation: There is no mountain.

Constraints:

  • 1 <= arr.length <= 104
  • 0 <= arr[i] <= 104

Follow up:

  • Can you solve it using only one pass?
  • Can you solve it in O(1) space?
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
G
G
Google
B
Bloomberg
N
Niantic

Approach

The goal of Longest Mountain in Array is to identify the longest contiguous subarray that strictly increases and then strictly decreases, forming a valid "mountain". A mountain must have at least three elements with a clear peak in the middle.

A common strategy is to analyze the array using dynamic programming or two-pointer enumeration. One idea is to precompute the length of increasing sequences ending at each index and decreasing sequences starting from each index. If both values are greater than zero at a position, that index can serve as the peak of a mountain. The total mountain length can then be derived from these two values.

Another efficient method uses a two-pointer or single-pass scan to expand around potential peaks while tracking increasing and decreasing slopes. Both approaches ensure that every element is processed only a limited number of times, leading to an overall O(n) time complexity with O(n) or O(1) additional space depending on the implementation.

Complexity

ApproachTime ComplexitySpace Complexity
Dynamic Programming (increasing & decreasing arrays)O(n)O(n)
Two Pointers / Single Pass EnumerationO(n)O(1)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Two-Pass Approach with Peak Detection

This approach involves scanning the array to find all the peaks and then measuring the length of a mountain centered at each peak. We use two traversals: one forward scan to detect peaks and another scan to calculate maximum width of the mountains.

Time complexity is O(n) as each element is processed at most twice. Space complexity is O(1) since we use only a few extra variables.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int longestMountain(int* arr, int arrSize) {
4    if (arrSize < 3) return 0;
5    int maxLen = 0;
6    for (int i = 1; i < arrSize - 1; ) {
7        if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
8            int left = i - 1, right = i + 1;
9            while (left > 0 && arr[left] > arr[left - 1]) left--;
10            while (right < arrSize - 1 && arr[right] > arr[right + 1]) right++;
11            maxLen = fmax(maxLen, right - left + 1);
12            i = right + 1;
13        } else {
14            i++;
15        }
16    }
17    return maxLen;
18}
19
20int main() {
21    int arr[] = {2, 1, 4, 7, 3, 2, 5};
22    int arrSize = sizeof(arr) / sizeof(arr[0]);
23    printf("%d\n", longestMountain(arr, arrSize));
24    return 0;
25}

Explanation

The C solution iterates through the array looking for peaks (where arr[i] is greater than its neighbors). Upon finding a peak, it expands outwards to calculate the total length of the mountain by decrementing and incrementing indices as long as the mountain shape holds. The maxLen keeps track of the longest mountain found.

Single-Pass Approach with Gradient Tracking

This approach uses a single pass through the array to maintain both ascent and descent counts, swapping them at every ascent reset. A separate check is performed to ensure valid peaks for mountain length calculations.

Time complexity is O(n) for a single well-managed loop, with O(1) space thanks to a fixed set of variables.

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

Leetcode 128 - LONGEST CONSECUTIVE SEQUENCE

NeetCode
9:24455,447 views

Asked By Companies

4 companies
G
Goldman Sachs
G
Google
B
Bloomberg
N
Niantic

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

Container With Most WaterMedium
3SumMedium
3Sum ClosestMedium
4SumMedium
More similar problems

Related Topics

ArrayTwo PointersDynamic ProgrammingEnumeration

Problem Stats

Acceptance Rate40.7%
DifficultyMedium
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why must a mountain have at least three elements?

A valid mountain requires a strictly increasing sequence followed by a strictly decreasing sequence. This structure needs at least three elements: one for the ascent, one peak element, and one for the descent.

Is Longest Mountain in Array asked in FAANG interviews?

Yes, variations of this problem appear in technical interviews at large tech companies. It tests array traversal, pattern recognition, and the ability to optimize from brute force to linear-time solutions.

What is the optimal approach for Longest Mountain in Array?

The optimal approach scans the array to detect increasing and decreasing slopes around potential peaks. Many solutions either precompute increasing and decreasing lengths or use a single-pass two-pointer technique. Both approaches achieve O(n) time complexity.

What data structure is best for solving Longest Mountain in Array?

The problem mainly relies on array traversal rather than complex data structures. Simple arrays for dynamic programming or pointer variables for a single-pass scan are sufficient to track increasing and decreasing sequences.

include
<stdio.h>
2
3
int
longestMountain
(
int
*
arr
,
int
arrSize
)
{
4
int
i
=
1
,
maxLen
=
0
,
ascent
=
0
,
descent
=
0
;
5
while
(
i
<
arrSize
)
{
6
while
(
i
<
arrSize
&&
arr
[
i
]
==
arr
[
i
-
1
]
)
i
++
;
7
while
(
i
<
arrSize
&&
arr
[
i
]
>
arr
[
i
-
1
]
)
ascent
++
,
i
++
;
8
while
(
i
<
arrSize
&&
ascent
>
0
&&
arr
[
i
]
<
arr
[
i
-
1
]
)
descent
++
,
i
++
;
9
if
(
ascent
>
0
&&
descent
>
0
)
10
maxLen
=
fmax
(
maxLen
,
ascent
+
descent
+
1
)
;
11
ascent
=
descent
=
0
;
12
}
13
return
maxLen
;
14
}
15
16
int
main
(
)
{
17
int
arr
[
]
=
{
2
,
1
,
4
,
7
,
3
,
2
,
5
}
;
18
int
arrSize
=
sizeof
(
arr
)
/
sizeof
(
arr
[
0
]
)
;
19
printf
(
"%d\n"
,
longestMountain
(
arr
,
arrSize
)
)
;
20
return
0
;
21
}

Explanation

This C implementation leverages variable ascent to track climbing phase and descent for descent. A valid mountain forms when both ascent and descent qualities exceed zero. The inner loop skips flat sections to align with mountain criteria.