Skip to content

Commit e9992a4

Browse files
committed
Add solution #1502
1 parent bd09409 commit e9992a4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
1486|[XOR Operation in an Array](./1486-xor-operation-in-an-array.js)|Easy|
175175
1492|[The kth Factor of n](./1492-the-kth-factor-of-n.js)|Medium|
176176
1496|[Path Crossing](./1496-path-crossing.js)|Easy|
177+
1502|[Can Make Arithmetic Progression From Sequence](./1502-can-make-arithmetic-progression-from-sequence.js)|Easy|
177178
1598|[Crawler Log Folder](./1598-crawler-log-folder.js)|Easy|
178179
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
179180
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 1502. Can Make Arithmetic Progression From Sequence
3+
* https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/
4+
* Difficulty: Easy
5+
*
6+
* A sequence of numbers is called an arithmetic progression if the difference
7+
* between any two consecutive elements is the same.
8+
*
9+
* Given an array of numbers arr, return true if the array can be rearranged to
10+
* form an arithmetic progression. Otherwise, return false.
11+
*/
12+
13+
/**
14+
* @param {number[]} arr
15+
* @return {boolean}
16+
*/
17+
var canMakeArithmeticProgression = function(arr) {
18+
arr.sort((a, b) => a - b);
19+
20+
for (let i = 2, diff = arr[1] - arr[0]; i < arr.length; i++) {
21+
if (arr[i] - arr[i - 1] !== diff) {
22+
return false;
23+
}
24+
}
25+
26+
return true;
27+
};

0 commit comments

Comments
 (0)