Algorithm to generate mountain ranges with upstrokes and down-strokes java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Generating mountain ranges with upstrokes (/) and down-strokes (\) is a classic combinatorics problem equivalent to generating valid parentheses or Catalan number sequences. Given n pairs, the goal is to produce all strings of n upstrokes and n down-strokes where at no prefix do down-strokes exceed upstrokes (the "mountain" never goes below ground level). The number of valid mountain ranges of n pairs is the nth Catalan number. Backtracking recursion is the standard approach.
The Problem
For n = 3, the valid mountain ranges are:
Represented as strings: ///\\\, //\/\\, //\\/\, /\/\/\, /\//\\
Each string has exactly n forward slashes and n backslashes, and at every position, the count of / characters is greater than or equal to the count of \ characters.
Recursive Backtracking Solution
The key constraint is down < up — you can only add a down-stroke if the current height (upstrokes minus down-strokes) is above zero.
Visual ASCII Rendering
Iterative Approach with Stack
The iterative version replaces recursion with an explicit stack, avoiding StackOverflowError for large n.
Counting with Catalan Numbers
The nth Catalan number C(n) = (2n)! / ((n+1)! * n!) gives the count without generating all sequences.
Common Pitfalls
- Allowing down-strokes below ground level: The constraint is
down < up, notdown < n. Without this check, invalid sequences like\/(going negative) are produced. - String concatenation in recursion: Creating new
Stringobjects at each recursive call is O(n) per call. UseStringBuilderwith append/delete for O(1) per step. - Stack overflow for large n: Recursive solutions hit Java's default stack limit (~512-1024 frames) around n=500. Use the iterative approach or increase stack size with
-Xss. - Confusing mountain ranges with balanced parentheses: They are mathematically identical —
/maps to(and\maps to). The same algorithm generates both. - Not escaping backslash in output: In Java strings, backslash must be escaped as
\\. When printing mountain ranges,System.out.println("\\")outputs a single\.
Summary
- Mountain ranges with
/and\are equivalent to balanced parentheses (Catalan numbers) - Use recursive backtracking with the constraint
down < upto ensure validity - Use
StringBuilderfor efficient string building in recursion - The count of valid ranges for
npairs is the nth Catalan number: C(n) = (2n)! / ((n+1)! * n!) - Use iterative approach with explicit stack for large
nto avoid stack overflow - ASCII rendering maps strokes to a 2D grid based on current height
Related reading
- Algorithm to generate random 2D polygon
- Algorithm to generate RGB graduated colors in PHP
- Algorithm to generate spanning set
- Algorithm to get all possible string combinations from array up to certain length
- Alternative to openjdk8-alpine for Kafka Streams
- Amazon S3 Signature Does Not Match - AWS SDK Java
- Algorithm to get the excel-like column name of a number
- Algorithm to group items in groups of 3

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.