Tap here to switch tabs
Problem
Submissions

Problem: Maximum Value of an Alternating Sequence

Maximum Value of an Alternating Sequence defines a sequence of a given length that starts at a given value, adds an amount, and subtracts one in turn. Return its largest value.

Statement

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

Construct a sequence of length n starting with s, where the elements a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} are defined as follows:

  • a0=sa_0 = s

  • For each ii from 11 to n−1n - 1:

    • If ii is odd, ai=ai−1+ma_i = a_{i - 1} + m

    • If ii is even, ai=ai−1−1a_i = a_{i - 1} - 1

Return the maximum value present in the sequence.

Constraints:

  • 1≤1 \leq n, s ≤109\leq 10^9

  • 1≤1 \leq m ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Value of an Alternating Sequence

Maximum Value of an Alternating Sequence defines a sequence of a given length that starts at a given value, adds an amount, and subtracts one in turn. Return its largest value.

Statement

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

Construct a sequence of length n starting with s, where the elements a0,a1,…,an−1a_0, a_1, \dots, a_{n - 1} are defined as follows:

  • a0=sa_0 = s

  • For each ii from 11 to n−1n - 1:

    • If ii is odd, ai=ai−1+ma_i = a_{i - 1} + m

    • If ii is even, ai=ai−1−1a_i = a_{i - 1} - 1

Return the maximum value present in the sequence.

Constraints:

  • 1≤1 \leq n, s ≤109\leq 10^9

  • 1≤1 \leq m ≤105\leq 10^5