1502. Can Make Arithmetic Progression From Sequence LeetCode Solution

In this guide, you will get 1502. Can Make Arithmetic Progression From Sequence LeetCode Solution with the best time and space complexity. The solution to Can Make Arithmetic Progression From Sequence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Can Make Arithmetic Progression From Sequence solution in C++
  4. Can Make Arithmetic Progression From Sequence solution in Java
  5. Can Make Arithmetic Progression From Sequence solution in Python
  6. Additional Resources
1502. Can Make Arithmetic Progression From Sequence LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: arr = [3,5,1]
Output: true
Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements.

Example 2:

Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.

Constraints:

2 <= arr.length <= 1000
-106 <= arr[i] <= 106

Complexity Analysis

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

1502. Can Make Arithmetic Progression From Sequence LeetCode Solution in C++

class Solution {
 public:
  bool canMakeArithmeticProgression(vector<int>& arr) {
    const int n = arr.size();
    const int mx = ranges::max(arr);
    const int mn = ranges::min(arr);
    const int range = mx - mn;
    if (range % (n - 1) != 0)
      return false;
    const int diff = range / (n - 1);
    if (diff == 0)
      return true;

    unordered_set<int> seen;

    for (const int a : arr) {
      if ((a - min) % diff != 0)
        return false;
      if (!seen.insert(a).second)
        return false;
    }

    return true;
  }
};
/* code provided by PROGIEZ */

1502. Can Make Arithmetic Progression From Sequence LeetCode Solution in Java

class Solution {
 public:
  bool canMakeArithmeticProgression(vector<int>& arr) {
    const int n = arr.size();
    const int mx = ranges::max(arr);
    const int mn = ranges::min(arr);
    const int range = mx - mn;
    if (range % (n - 1) != 0)
      return false;
    const int diff = range / (n - 1);
    if (diff == 0)
      return true;

    for (int i = 0; i < n;) {
      const int gap = arr[i] - mn;
      if (gap % diff != 0)
        return false;
      if (gap == i * diff) {
        ++i;
      } else {
        const int rightIndex = gap / diff;
        swap(arr[i], arr[rightIndex]);
      }
    }

    return true;
  }
};
// code provided by PROGIEZ

1502. Can Make Arithmetic Progression From Sequence LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  2177. Find Three Consecutive Integers That Sum to a Given Number LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.