FastPrepDigit-Sum Sequence Join Point

Digit-Sum Sequence Join Point

OpenAI logoOpenAI● EasyINTERNOA
Learn

Problem statement

Starting from a positive integer, build a sequence by repeatedly adding the sum of its decimal digits to the current value. For example, after 471 comes 471 + 4 + 7 + 1 = 483.

You are given the starting values s1 and s2 of two such sequences. It is guaranteed that the sequences share a value strictly below 20,000,000.

Return their first common value: the smallest integer that appears in both sequences. Each starting value is itself part of its sequence.

Function

computeJoinPoint(s1: int, s2: int) → int

Examples

Example 1

s1 = 471s2 = 480return = 519

The first sequence begins 471, 483, 498, 519, 534. The second begins 480, 492, 507, 519, 534. Their first common value is 519.

Example 2

s1 = 37s2 = 37return = 37

Both sequences already start at the same value, so the first common value is 37.

Example 3

s1 = 91s2 = 100return = 101

After 91 comes 91 + 9 + 1 = 101. After 100 comes 100 + 1 = 101. Neither starting value belongs to the other sequence before this, so the first common value is 101.

Constraints

  • 1 <= s1, s2 < 20,000,000.
  • Each next value equals the current value plus the sum of its decimal digits.
  • The two sequences have a common value strictly below 20,000,000.

More OpenAI problems

See OpenAI hiring insights
public int computeJoinPoint(int s1, int s2) {
    // Return the first value that belongs to both sequences.
}
s1471
s2480
expected519
Checking account…