LeetCode 1228: Missing Number In Arithmetic Progression Solution

Master LeetCode problem 1228 (Missing Number In Arithmetic Progression), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1228. Missing Number In Arithmetic Progression

Problem Explanation

Explanation:

To find the missing number in an arithmetic progression, we can calculate the common difference between the elements and then identify the missing number based on the expected progression. We can do this by calculating the actual common difference between the first two elements and comparing it with the expected common difference. Once we have the common difference, we can find the missing number by iterating through the elements and checking for any gaps in the progression.

Algorithm:

  1. Calculate the common difference between the first two elements.
  2. Calculate the expected common difference based on the total number of elements.
  3. Iterate through the elements to find the missing number based on the expected progression.

Time Complexity: O(n) Space Complexity: O(1)

: :

Solution Code

class Solution {
    public int missingNumber(int[] arr) {
        int n = arr.length;
        int commonDiff = (arr[n-1] - arr[0]) / n;
        
        for (int i = 1; i < n; i++) {
            if (arr[i] != arr[i-1] + commonDiff) {
                return arr[i-1] + commonDiff;
            }
        }
        
        return arr[0];
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1228 (Missing Number In Arithmetic Progression)?

This page provides optimized solutions for LeetCode problem 1228 (Missing Number In Arithmetic Progression) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1228 (Missing Number In Arithmetic Progression)?

The time complexity for LeetCode 1228 (Missing Number In Arithmetic Progression) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1228 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1228 in Java, C++, or Python.

Back to LeetCode Solutions