Skip to main content

Stack Exchange Network

Stack Exchange network consists of 184 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
The 2026 Annual Developer Survey is live— take the Survey today!.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Reconstruct an arithmetic sequence

Given a finite arithmetic sequence of positive integers with some terms removed from the middle, reconstruct the whole sequence.

The task

Consider an arithmetic sequence: a list of positive integers in which the difference between any two successive elements is the same.

2 5 8 11 14 17

Now suppose one or more integers is removed from the sequence, subject to the following constraints:

  • The integers removed will be consecutive terms of the sequence.
  • The first and last integers in the sequence will not be removed.
  • At least three integers will remain in the sequence.

For the above sequence, possible removals include:

2 5 8 14 17  (removed 11)
2 5 17       (removed 8 11 14)
2 14 17      (removed 5 8 11)

Your task: Given one of these partial sequences, reconstruct the original full sequence.

Details

You may assume input is valid (has a solution) and is missing at least one term. All numbers in the sequence will be positive (> 0) integers. The sequence may have a positive or negative difference between terms (i.e. it may be increasing or decreasing). It will not be a constant sequence (e.g. 5 5 5).

Your solution may be a full program or a function. Any of the default input and output methods are acceptable.

Your input and output may be a string (with any reasonable delimiter), a list of strings, or a list of numbers. You may represent the numbers in whatever base is convenient for your language.

Please mention any unusual I/O methods/formats in your submission, so others will be able to test your code more easily.

Test cases

In: 2 5 8 14 17
Out: 2 5 8 11 14 17
In: 2 5 17
Out: 2 5 8 11 14 17
In: 2 14 17
Out: 2 5 8 11 14 17
In: 21 9 6 3
Out: 21 18 15 12 9 6 3
In: 10 9 5
Out: 10 9 8 7 6 5
In: 1 10 91 100
Out: 1 10 19 28 37 46 55 64 73 82 91 100

This is ; the shortest answer in each language wins.

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
2
  • \$\begingroup\$ Yay! Beats the 05AB1E answer by 1 byte! \$\endgroup\$
    user202729
    –  user202729
    2017-11-22 08:56:51 +00:00
    Commented Nov 22, 2017 at 8:56
  • \$\begingroup\$ Using gcd instead of min made us tie. Too bad I get a gcd with sign, otherwise I'd be at 7 ;) \$\endgroup\$
    Emigna
    –  Emigna
    2017-11-22 10:26:11 +00:00
    Commented Nov 22, 2017 at 10:26

Morty Proxy This is a proxified and sanitized view of the page, visit original site.