FastPrepStarting Value with the Longest Collatz Chain

Starting Value with the Longest Collatz Chain

ZipRecruiter logoZipRecruiter● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

For a positive value n, repeatedly replace an even value by n / 2 and an odd value by 3n + 1 until reaching 1.

Among starting values from 1 through limit, return the one with the longest chain, counting both the start and 1. Break equal lengths by the smaller starting value.

Function

longestCollatzStart(limit: int) → int

Examples

Example 1

limit = 1return = 1

Only starting value one is available.

Example 2

limit = 2return = 2

Two has a longer chain than one.

Constraints

  • 1 <= limit <= 1000000
  • Intermediate values fit in signed 64-bit integers.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int longestCollatzStart(int limit) {
    // Write your code here.
}
limit1
expected1
Checking account…