-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e007d98
commit 792d625
Showing
1 changed file
with
16 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
leetcode/problems/java/check-if-array-pairs-are-divisible-by-k.java
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,16 @@ | ||
class Solution { | ||
public boolean canArrange(int[] arr, int k) { | ||
int freq[] = new int[k]; | ||
for (int n : arr) | ||
freq[(n % k + k) % k]++; | ||
|
||
if (freq[0] % 2 != 0) // k - 0 = k and it does not exist in the array | ||
return false; | ||
|
||
for (int i = 1; i <= k / 2; i++) | ||
if (freq[i] != freq[k - i]) | ||
return false; | ||
|
||
return true; | ||
} | ||
} |