bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1228. Missing Number In Arithmetic Progression

1228. Missing Number In Arithmetic Progression

Leetcode Solutions

Approach: Linear Search

  1. Calculate the common difference using the formula difference = (arr[-1] - arr[0]) / len(arr).
  2. Initialize the expected value to the first element of the array expected = arr[0].
  3. Iterate through the array starting from the first element.
  4. For each index i, calculate the expected value at that index expected += difference.
  5. If the actual value at index i does not match the expected value, return the expected value as the missing number.
  6. If all values match, then the missing number is after the last element, so return the last element plus the difference.
UML Thumbnail

Approach: Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...