Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 156c1e1

Browse files
committed
Add solution #1497
1 parent f0bec56 commit 156c1e1

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,329 LeetCode solutions in JavaScript
1+
# 1,330 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1144,6 +1144,7 @@
11441144
1493|[Longest Subarray of 1's After Deleting One Element](./solutions/1493-longest-subarray-of-1s-after-deleting-one-element.js)|Medium|
11451145
1494|[Parallel Courses II](./solutions/1494-parallel-courses-ii.js)|Hard|
11461146
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|
11471148
1502|[Can Make Arithmetic Progression From Sequence](./solutions/1502-can-make-arithmetic-progression-from-sequence.js)|Easy|
11481149
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11491150
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
};

0 commit comments

Comments
 (0)