LeetCode in Kotlin

3993. Maximum Value of an Alternating Sequence

Medium

You are given three integers n, s, and m.

A sequence seq of integers of length n is considered valid if:

A sequence of length 1 is considered alternating.

Return the maximum possible element that can appear in any valid sequence.

Example 1:

Input: n = 4, s = 3, m = 5

Output: 12

Explanation:

Example 2:

Input: n = 2, s = 4, m = 3

Output: 7

Explanation:

Constraints:

Solution

class Solution {
    fun maximumValue(n: Int, s: Int, m: Int): Long {
        if (n == 1) {
            return s.toLong()
        }
        return s + (n / 2).toLong() * (m - 1) + 1
    }
}