Lexicographically Largest Mountain List - Amazon Top Interview Questions


Problem Statement :


You are given three positive integers n, lower, and upper. You want to create a list of length n that is strictly increasing and then strictly decreasing and all the numbers are between [lower, upper], inclusive. Each of the increasing and decreasing parts should be non-empty.

Return the lexicographically largest list possible, or the empty list if it's not possible.

Constraints

3 ≤ n ≤ 100,000
1 ≤ lower ≤ upper < 2 ** 31


Example 1

Input

n = 5
lower = 2
upper = 6

Output

[5, 6, 5, 4, 3]


Explanation

Note that [6, 5, 4, 3, 2] is not valid since the strictly increasing part has to be non-empty.



Example 2

Input

n = 5
lower = 90
upper = 92

Output
[90, 91, 92, 91, 90]



Example 3

Input

n = 6
lower = 3
upper = 5


Output
[]


Explanation

It's impossible to make a strictly increasing then decreasing list of size 6 here.



Solution :



title-img




                        Solution in C++ :

vector<int> solve(int n, int lower, int upper) {
    deque<int> d;
    int num = upper;
    for (int i = 0; i < n - 1; ++i) {
        if (num < lower) break;
        d.push_back(num--);
    }
    int right = d.size();
    num = upper - 1;
    for (int i = 0; i < n - right; ++i) {
        if (num < lower) break;
        d.push_front(num--);
    }
    if (d.size() != n) return {};
    vector<int> res;
    for (int i = 0; i < d.size(); ++i) res.push_back(d[i]);
    return res;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int[] solve(int N, int lower, int upper) {
        int[] ans = new int[N];
        if (N < 3 || lower == upper) {
            ans = new int[] {};
        } else if (N <= upper - lower + 2) {
            ans[0] = upper - 1;
            for (int i = 1; i < N; i++) ans[i] = upper - i + 1;
        } else if (N <= 2 * (upper - lower) + 1) {
            int cur = lower;
            int dir = 1;
            for (int i = N - 1; i >= 0; i--) {
                ans[i] = cur;
                if (cur == upper)
                    dir = -1;
                cur += dir;
            }
        } else {
            ans = new int[] {};
        }
        return ans;
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, n, lower, upper):
        i = j = upper - 1
        n -= 3
        while j > lower and n:
            j -= 1
            n -= 1
        while i > lower and n:
            i -= 1
            n -= 1

        if n or lower == upper:
            return []
        return list(range(i, upper)) + list(range(upper, j - 1, -1))
                    


View More Similar Problems

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →

Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →

Tree: Huffman Decoding

Huffman coding assigns variable length codewords to fixed length input characters based on their frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only t

View Solution →

Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

View Solution →

Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

View Solution →