-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution and test-cases for problem 1497
- Loading branch information
Showing
3 changed files
with
67 additions
and
11 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
leetcode/1401-1500/1497.Check-If-Array-Pairs-Are-Divisible-by-k/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# [1497.Check If Array Pairs Are Divisible by k][title] | ||
|
||
## Description | ||
Given an array of integers `arr` of even length `n` and an integer `k`. | ||
|
||
We want to divide the array into exactly `n / 2` pairs such that the sum of each pair is divisible by `k`. | ||
|
||
Return `true` If you can find a way to do that or `false` otherwise. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: arr = [1,2,3,4,5,10,6,7,8,9], k = 5 | ||
Output: true | ||
Explanation: Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10). | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: arr = [1,2,3,4,5,6], k = 7 | ||
Output: true | ||
Explanation: Pairs are (1,6),(2,5) and(3,4). | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: arr = [1,2,3,4,5,6], k = 10 | ||
Output: false | ||
Explanation: You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/check-if-array-pairs-are-divisible-by-k | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
20 changes: 18 additions & 2 deletions
20
leetcode/1401-1500/1497.Check-If-Array-Pairs-Are-Divisible-by-k/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,21 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(arr []int, k int) bool { | ||
count := make([]int, k) | ||
for _, n := range arr { | ||
t := n % k | ||
if n < 0 { | ||
t = k - (-n % k) | ||
if t == k { | ||
t = 0 | ||
} | ||
} | ||
count[t]++ | ||
} | ||
for i := 1; i < k; i++ { | ||
if count[i] != count[k-i] { | ||
return false | ||
} | ||
} | ||
return count[0]%2 == 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters