Can Make Arithmetic Progression From Sequence
In this lab, you will implement a function canMakeArithmeticProgression
that takes an array of numbers and determines if the array can be rearranged to form an arithmetic progression. An arithmetic progression is a sequence of numbers where the difference between any two consecutive elements is the same.
The function should return true
if the array can be rearranged to form an arithmetic progression, and false
otherwise. You should use ESM import/export everywhere in your code.
Function signature
/** * @param {number[]} arr * @return {boolean} */ var canMakeArithmeticProgression = function(arr) { };
Example
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 element.
Input: arr = [1,2,4]
Output: false
Explanation: There is no way to reorder the elements to obtain an arithmetic progression.
Input: arr = [7,9,11,13]
Output: true
Explanation: The array is already in arithmetic progression with a difference of 2 between consecutive elements.
Constraints
2 <= arr.length <= 1000
-106 <= arr[i] <= 106
Challenges
- Implement the
canMakeArithmeticProgression
function. - Export the
canMakeArithmeticProgression
function.
In the evaluation script, ensure that the order of challenge testing matches with the challenges mentioned. The final length of the testlog
array should be equal to the number of challenges.
If you are using any variables or functions inside the evaluation script that the user must provide in their code, make sure to mention it in the respective challenge block text.
Always import user code dynamically in each challenge's code block inside the evaluation script to avoid the lab crashing if the file(s) are not present.
Do not forget to add any new packages you are installing to the dependencies or devDependencies inside your package.json file in the solution.