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

962. Maximum Width Ramp

Medium55.5% Acceptance
ArrayStackMonotonic Stack
Asked by:
G
Google
ProblemSolutions (4)VideosCompanies (2)Notes

Problem Statement

A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.

Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.

Example 1:

Input: nums = [6,0,8,2,1,5]
Output: 4
Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.

Example 2:

Input: nums = [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.

Constraints:

  • 2 <= nums.length <= 5 * 104
  • 0 <= nums[i] <= 5 * 104
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
A
Amazon

Approach

The goal of #962 Maximum Width Ramp is to find the largest distance j - i such that i < j and nums[i] <= nums[j]. A brute-force approach checks all pairs, but it leads to O(n^2) time, which is inefficient for large arrays.

An optimized method uses a monotonic decreasing stack. First, traverse the array from left to right and push indices onto a stack only when they create a new smaller value. This stack keeps potential starting points for ramps in decreasing order of values. Then traverse the array from right to left, and whenever the current value satisfies the ramp condition with the top of the stack, calculate the width and update the maximum. Continue popping while valid ramps exist.

This strategy works because the stack stores only the most promising starting indices. The two-pass process ensures each element is processed efficiently, resulting in linear time complexity with minimal additional space.

Complexity

ApproachTime ComplexitySpace Complexity
Brute Force Pair CheckingO(n^2)O(1)
Monotonic Decreasing StackO(n)O(n)

Video Solution Available

NeetCodeIO

View all video solutions

Solutions (4)

Monotonic Stack Approach

This approach utilizes a stack to keep track of indices in a monotonically decreasing order, which helps to quickly find the maximum possible ramp width.

Time Complexity: O(n), where n is the number of elements in the array - because each element is pushed and popped from the stack at most once.
Space Complexity: O(n) for the stack of indices.

PythonC++
1def maxWidthRamp(nums):
2    stack = []
3    max_width = 0
4
5    # Build initial decreasing stack of indices
6    for i, num in enumerate(nums):
7        if not stack or nums[stack[-1]] > num:
8            stack.append(i)
9
10    # Traverse from the end for maximum width
11    for j in range(len(nums) - 1, -1, -1):
12        while stack and nums[stack[-1]] <= nums[j]:
13            max_width = max(max_width, j - stack.pop())
14
15    return max_width

Explanation

This solution first creates a stack that holds indices of nums in a decreasing manner. It then traverses the array from right to left, popping elements from the stack to calculate potential maximum widths.

Two Pointer Approach

This approach uses two pointers to find the maximum width ramp by initially sorting potential endpoints and then finding the earliest start point that forms a valid ramp.

Time Complexity: O(n log n) due to sorting.
Space Complexity: O(n) for storing indices.

JavaC#
1import java.util.ArrayList;
2import java.

Video Solutions

Watch expert explanations and walkthroughs

Maximum Width Ramp - Leetcode 962 - Python

NeetCodeIO
14:4720,039 views

Asked By Companies

2 companies
G
Google
A
Amazon

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

Trapping Rain WaterHard
Largest Rectangle in HistogramHard
Maximal RectangleHard
Evaluate Reverse Polish NotationMedium
More similar problems

Related Topics

ArrayStackMonotonic Stack

Problem Stats

Acceptance Rate55.5%
DifficultyMedium
Companies2

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Maximum Width Ramp asked in FAANG interviews?

Yes, variations of array and monotonic stack problems like Maximum Width Ramp can appear in FAANG-style interviews. They test understanding of stack patterns, greedy reasoning, and array traversal optimizations.

Why use a monotonic stack in Maximum Width Ramp?

A monotonic stack ensures that only useful candidate indices are stored. This reduces unnecessary comparisons and allows the algorithm to find the maximum ramp width in linear time.

What data structure is best for Maximum Width Ramp?

A monotonic stack is the most effective data structure for this problem. It helps track indices with decreasing values, allowing efficient comparison with elements scanned from the right side.

What is the optimal approach for Maximum Width Ramp?

The optimal approach uses a monotonic decreasing stack. First, store candidate starting indices while maintaining decreasing values, then scan from right to left to compute the maximum ramp width efficiently.

util
.
List
;
3
4
class
Solution
{
5
public
int
maxWidthRamp
(
int
[
]
nums
)
{
6
List
<
Integer
>
indices
=
new
ArrayList
<
>
(
)
;
7
for
(
int
i
=
0
;
i
<
nums
.
length
;
++
i
)
{
8
indices
.
add
(
i
)
;
9
}
10
indices
.
sort
(
(
a
,
b
)
->
Integer
.
compare
(
nums
[
a
]
,
nums
[
b
]
)
)
;
11
12
int
maxRamp
=
0
;
13
int
minIndex
=
nums
.
length
;
14
for
(
int
index
:
indices
)
{
15
maxRamp
=
Math
.
max
(
maxRamp
,
index
-
minIndex
)
;
16
minIndex
=
Math
.
min
(
minIndex
,
index
)
;
17
}
18
return
maxRamp
;
19
}
20
}

Explanation

This solution sorts the indices based on their values and then iteratively updates the maximum width by finding the smallest index encountered so far, to ensure the widest ramp.