Ctrl K

Peak of the Halving Sequence

Easy
A sequence s is defined by these rules: - s[0] = 0 and s[1] = 1; - for an even index 2k ≥ 2: s[2k] = s[k]; - for an odd index 2k + 1 ≥ 3: s[2k + 1] = s[k] + s[k + 1]. Given a number last, return the largest value among the terms s[0], s[1], …, s[last].
Example 1
last=9 → 4

Terms 0 through 9 are 0, 1, 1, 2, 1, 3, 2, 3, 1, 4. For instance, s[9] = s[4] + s[5] = 1 + 3 = 4. The largest is 4.

Example 2
last=4 → 2

The sequence is 0, 1, 1, 2, 1, so the maximum is 2.

Example 3
last=0 → 0
Constraints
  • 0 ≤ last ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsHow Long the Light Stayed On EasyRepack the Tray EasyQuiz Score Log EasyMirror and Negate a Bitmap EasySwap Rows and Columns Easy
loading Python…
Input
last=9
Expected
4

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