- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

5, 3, 7

[6, 7, 6, 5, 4]

- Related Questions & Answers
- 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
- Find the lexicographically largest palindromic Subsequence of a String in Python
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Python program to find N largest elements from a list
- Python program to find the second largest number in a list
- Program to find lexicographically smallest non-palindromic string in Python
- Python Program to Find the Largest Element in a Doubly Linked List
- Program to find largest kth index value of one list in Python
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- 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

Advertisements