File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,329 LeetCode solutions in JavaScript
1
+ # 1,330 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1144
1144
1493|[ Longest Subarray of 1's After Deleting One Element] ( ./solutions/1493-longest-subarray-of-1s-after-deleting-one-element.js ) |Medium|
1145
1145
1494|[ Parallel Courses II] ( ./solutions/1494-parallel-courses-ii.js ) |Hard|
1146
1146
1496|[ Path Crossing] ( ./solutions/1496-path-crossing.js ) |Easy|
1147
+ 1497|[ Check If Array Pairs Are Divisible by k] ( ./solutions/1497-check-if-array-pairs-are-divisible-by-k.js ) |Medium|
1147
1148
1502|[ Can Make Arithmetic Progression From Sequence] ( ./solutions/1502-can-make-arithmetic-progression-from-sequence.js ) |Easy|
1148
1149
1507|[ Reformat Date] ( ./solutions/1507-reformat-date.js ) |Easy|
1149
1150
1512|[ Number of Good Pairs] ( ./solutions/1512-number-of-good-pairs.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1497. Check If Array Pairs Are Divisible by k
3
+ * https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of integers arr of even length n and an integer k.
7
+ *
8
+ * We want to divide the array into exactly n / 2 pairs such that the sum of each pair is
9
+ * divisible by k.
10
+ *
11
+ * Return true If you can find a way to do that or false otherwise.
12
+ */
13
+
14
+ /**
15
+ * @param {number[] } arr
16
+ * @param {number } k
17
+ * @return {boolean }
18
+ */
19
+ var canArrange = function ( arr , k ) {
20
+ const remainders = new Array ( k ) . fill ( 0 ) ;
21
+
22
+ for ( const num of arr ) {
23
+ const remainder = ( ( num % k ) + k ) % k ;
24
+ remainders [ remainder ] ++ ;
25
+ }
26
+
27
+ for ( let i = 0 ; i <= Math . floor ( k / 2 ) ; i ++ ) {
28
+ if ( i === 0 || i * 2 === k ) {
29
+ if ( remainders [ i ] % 2 !== 0 ) return false ;
30
+ } else if ( remainders [ i ] !== remainders [ k - i ] ) {
31
+ return false ;
32
+ }
33
+ }
34
+
35
+ return true ;
36
+ } ;
You can’t perform that action at this time.
0 commit comments