🎉 One-stop destination for all your technical interview Preparation 🎉
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.
**SC: O(logN) | Â | O(N)**: Considering Space required for internal sorting. in C++ its O(logN) for py its O(N). |
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;
}
};