Problem · Greedy

Construct Winning Sequence

Learn this problem
MediumSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem statement

A challenge in a programming competition requires the construction of a sequence using a specified number of integers within a range. The sequence must be strictly increasing at first and then strictly decreasing. The goal is to maximize the sequence array elements starting from the beginning. For example, [4,5,4,3,2] beats [3,4,5,4,3] because its first element is larger, and [4,5,6,5,4] beats [4,5,4,3,2] because its third element is larger. Given the length of the sequence and the range of integers, return the winning sequence. If it is not possible to construct such a sequence, return -1.

Function

constructSequence(n: int, lo: int, hi: int) → int[]

Examples

Example 1

n = 5lo = 1hi = 2return = [-1]
It is not possible to construct a sequence that is strictly increasing and then strictly decreasing with the given range of integers.

Example 2

n = 5lo = 4hi = 11return = [10, 11, 10, 9, 8]
N/A

Example 3

n = 6lo = 5hi = 10return = [9, 10, 9, 8, 7, 6]
N/A

More Salesforce problems

drafts saved locally
public int[] constructSequence(int n, int lo, int hi) {
  // write your code here
}
n5
lo1
hi2
expected[-1]
checking account