- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find lexicographically largest mountain list in Python

Suppose we have three positive numbers say n, lower, and upper. We have to find a list whose length is n and that is strictly increasing and then strictly decreasing and all the numbers are in range [lower and upper] (both inclusive). And each increasing and decreasing parts should be non-empty. We have to find the lexicographically largest such list possible, if this is not possible, then return empty list.

So, if the input is like n = 5 lower = 3 upper = 7, then the output will be [6, 7, 6, 5, 4], if we look closely, the [7, 6, 5, 4, 3] is not valid because the strictly increasing part should be non-empty.

To solve this, we will follow these steps −

if n > 2 * (upper - lower) + 1, then

return empty list

c := upper - lower

d := 1

if c < n, then

d := n - c - 1

if d is same as 0, then

d := 1

f := a new list from range from (upper - d) to (upper - 1)

g := a new list from range (upper - n + d - 1) down to upper

concatenate f and g and return

## Example

Let us see the following implementation to get better understanding

def solve(n, lower, upper): if n > 2 * (upper - lower) + 1: return [] c = upper - lower d = 1 if c < n: d = n - c - 1 if d == 0: d = 1 f = list(range(upper - d, upper)) g = list(range(upper, upper - n + d, -1)) return f + g n = 5 lower = 3 upper = 7 print(solve(n, lower, upper))

## Input

5, 3, 7

## Output

[6, 7, 6, 5, 4]

- Related Articles
- Program to construct the lexicographically largest valid sequence in Python
- Python program to find largest number in a list
- Python program to find the largest number in a list
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Python program to find the second largest number in a list
- Python program to find N largest elements from a list
- Python Program to Find the Largest Element in a Doubly Linked List
- Program to find largest kth index value of one list in Python
- Program to find lexicographically smallest non-palindromic string in Python
- Program to find largest distance pair from two list of numbers in Python
- Program to find Lexicographically Smallest String With One Swap in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find lexicographically smallest string after applying operations in Python
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List