Repository navigation
Feature/hashes #11652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Feature/hashes #11652
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5526e9f
example for array_hashtable_counting
kvvCoutureAI 125138e
format using black and riff
kvvCoutureAI fafde10
add doctest
kvvCoutureAI 79dc703
change variable name
kvvCoutureAI 28594f0
add proper doctest
kvvCoutureAI df31ef3
Merge branch 'master' into feature/hashes
cclauss c1f9ff1
updating DIRECTORY.md
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,84 @@ | ||
| """ | ||
| check-if-array-pairs-are-divisible-by-k | ||
|
|
||
| Problem statement: | ||
| Given an array of integers arr of even length n and an integer k. | ||
|
|
||
| Divide the array into 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. | ||
| See that there is no way to divide arr into 3 pairs each with sum divisible by 10. | ||
|
|
||
| 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. | ||
|
|
||
|
|
||
| class Solution: | ||
| def can_arrange(self, arr: list[int], input_num: int) -> bool: | ||
| """ | ||
| Function to check if array pairs are divisible by k | ||
| :param arr: List of integers | ||
| :param k: Integer | ||
| :return: Boolean | ||
|
|
||
| Example: | ||
| >>> obj = Solution() | ||
| >>> obj.can_arrange([1, 2, 3, 4, 5, 10, 6, 7, 8, 9], 5) | ||
| True | ||
| >>> obj.can_arrange([1, 2, 3, 4, 5, 6], 7) | ||
| True | ||
| """ | ||
| sol_arr = [0 for i in range(input_num)] | ||
| for i in arr: | ||
| sol_arr[i % input_num] += 1 | ||
| if sol_arr[0] % 2 != 0: | ||
| return False | ||
| i, j = 1, input_num - 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] | ||
| input_num = 5 | ||
| s = Solution() | ||
| print(s.can_arrange(arr, input_num)) # Output: True | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
hashes/array_pairs_divisibility.py, please provide doctest for the functioncan_arrange