Ctrl K

Rotating Operator Countdown

Medium
Write the numbers from n down to 1 and put operators between neighbours in a repeating cycle: *, /, +, -, then *, /, +, - again, and so on. For n = 7 this gives 7 * 6 / 5 + 4 - 3 * 2 / 1. The expression follows the usual precedence: multiplication and division first, left to right, then addition and subtraction. Division is integer division rounded down: 7 * 6 / 5 = 42 / 5 = 8. Return the value of the expression for the given n.
Example 1
n=5 → 7

5 * 4 / 3 + 2 - 1: first 5 * 4 = 20, then 20 / 3 = 6 (rounded down), then 6 + 2 - 1 = 7.

Example 2
n=9 → 11

9 * 8 / 7 + 6 - 5 * 4 / 3 + 2 - 1 = 10 + 6 - 6 + 2 - 1 = 11.

Example 3
n=2 → 2

The expression is just 2 * 1.

Constraints
  • 1 ≤ n ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMultiplying Digit Strings MediumPostfix Calculator MediumFour-Operation Calculator MediumForward Digit List Sum MediumMultiply Complex Numbers in Strings Medium
loading Python…
Input
n=5
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 41 tests on the server, hidden ones included.