Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

1502. Can Make Arithmetic Progression From Sequence 🌟

A sequence of numbers is called an arithmetic progression if the difference between any two consecutive elements is the same.

Given an array of numbers arr, return true if the array can be rearranged to form an arithmetic progression. Otherwise, return false.

Simple Solution

Code

class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr)
    {
        sort(arr.begin(), arr.end());
        int n = arr.size();
        for (int i = 1; i < n - 1; i++) {
            if (arr[i - 1] - arr[i] != arr[i] - arr[i + 1])
                return false;
        }
        return true;
    }
};