File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 174
174
1486|[ XOR Operation in an Array] ( ./1486-xor-operation-in-an-array.js ) |Easy|
175
175
1492|[ The kth Factor of n] ( ./1492-the-kth-factor-of-n.js ) |Medium|
176
176
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|
177
178
1598|[ Crawler Log Folder] ( ./1598-crawler-log-folder.js ) |Easy|
178
179
1780|[ Check if Number is a Sum of Powers of Three] ( ./1780-check-if-number-is-a-sum-of-powers-of-three.js ) |Medium|
179
180
1880|[ Check if Word Equals Summation of Two Words] ( ./1880-check-if-word-equals-summation-of-two-words.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments