Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Missing Term in an Arithmetic Progression

Basic Acceptance 82.87% Points 10.00

You are given an arithmetic progression (AP) in which exactly one term is missing and represented by a placeholder (_). Your task is to find and return the missing term.

An arithmetic progression is a sequence in which the difference between consecutive terms remains constant. The given sequence is guaranteed to form a valid AP when the missing term is restored.

Examples
Example 1

Input: seq = [2, 4, _, 8, 10]

Output: 6

Explanation: The common difference is 2. Therefore, the missing term is 6.

Example 2

Input: seq = [5, 10, 15, _, 25]

Output: 20

Explanation: The common difference is 5. Therefore, the missing term is 20.

Example 3

Input: seq = [_, 3, 5, 7]

Output: 1

Explanation: The common difference is 2. The term immediately before 3 is 1.

Constraints
  • 3 <= number of known terms <= 105
  • The sequence is a valid AP apart from the one missing term.
Companies
Samsung Cisco
Topics
Introduction Data Structures Basics Array
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions