Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions hashes/array_pairs_divisibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'''
check-if-array-pairs-are-divisible-by-k

Problem statement:
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.

Check failure on line 7 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

hashes/array_pairs_divisibility.py:7:89: E501 Line too long (102 > 88)

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.

Check failure on line 25 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

hashes/array_pairs_divisibility.py:25:89: E501 Line too long (129 > 88)

Check failure on line 26 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

hashes/array_pairs_divisibility.py:26:1: W293 Blank line contains whitespace

Constraints:

arr.length == n
1 <= n <= 105
n is even.
-109 <= arr[i] <= 109
1 <= k <= 105
'''

# Time complexity: O(n)
# Space complexity: O(k)

# Approach:
# 1. Create a list of size k with all elements as 0.
# 2. For each element in the array, increment the element at index i%k.
# 3. If the element at index 0 is odd, return False.
# 4. Check if the number of elements at index i and k-i are equal.
# 5. If not, return False.
# 6. If all the conditions are satisfied, return True.

from typing import List

Check failure on line 48 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

hashes/array_pairs_divisibility.py:48:1: UP035 `typing.List` is deprecated, use `list` instead

class Solution:

Check failure on line 50 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

hashes/array_pairs_divisibility.py:48:1: I001 Import block is un-sorted or un-formatted
def canArrange(self, arr: List[int], k: int) -> bool:

Check failure on line 51 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

hashes/array_pairs_divisibility.py:51:9: N802 Function name `canArrange` should be lowercase

Check failure on line 51 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

hashes/array_pairs_divisibility.py:51:31: UP006 Use `list` instead of `List` for type annotation
sol_arr = [0 for i in range(0,k)]

Check failure on line 52 in hashes/array_pairs_divisibility.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE808)

hashes/array_pairs_divisibility.py:52:37: PIE808 Unnecessary `start` argument in `range`
for i in arr:
sol_arr[i%k]+=1
if sol_arr[0]%2!=0:
return False
i, j = 1, k-1
while(j>i):
if sol_arr[i]==sol_arr[j]:
j-=1
i+=1
else:
return False
return True

# Driver code
if __name__ == '__main__':
arr = [1,2,3,4,5,10,6,7,8,9]
k = 5
s = Solution()
print(s.canArrange(arr, k)) # Output: True
Loading