-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
largest divisible subset #9825
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
Merged
Merged
largest divisible subset #9825
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3fec355
largest divisible subset
pa-kh039 e96dfb5
minor tweaks
pa-kh039 e78e5f6
adding more test cases
pa-kh039 16ff978
improving code for better readability
pa-kh039 344a545
update
pa-kh039 4692827
update
pa-kh039 c8465fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 726d94b
suggested changes done, and further modfications
pa-kh039 09fde0e
final update
pa-kh039 a1bcbd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9c8b15f
Update largest_divisible_subset.py
cclauss 4cf3f07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d47a8a
Update largest_divisible_subset.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
|
||
def largest_divisible_subset(array: list[int]) -> list[int]: | ||
""" | ||
Algorithm to find the biggest subset | ||
in the given array such that for any | ||
2 elements x and y in the subset, | ||
either x divides y or y divides x | ||
>>> largest_divisible_subset([1,16,7,8,4]) | ||
[16, 8, 4, 1] | ||
>>> largest_divisible_subset([1,2,3]) | ||
[2, 1] | ||
>>> largest_divisible_subset([1, 2, 4, 8]) | ||
[8, 4, 2, 1] | ||
pa-kh039 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
array_size = len(array) | ||
|
||
# Sort the array in ascending order | ||
# as the sequence does not matter | ||
# we only have to pick up a subset | ||
array.sort() | ||
|
||
# Initialize memo and hash arrays with 1s | ||
memo = [1] * array_size | ||
hash_array = list(range(array_size)) | ||
|
||
# Iterate through the array | ||
for i in range(array_size): | ||
for prev_index in range(i): | ||
if array[i] % array[prev_index] == 0 and 1 + memo[prev_index] > memo[i]: | ||
pa-kh039 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memo[i] = 1 + memo[prev_index] | ||
hash_array[i] = prev_index | ||
|
||
ans = -1 | ||
last_index = -1 | ||
|
||
# Find the maximum length and its corresponding index | ||
for i in range(array_size): | ||
if memo[i] > ans: | ||
ans = memo[i] | ||
pa-kh039 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
last_index = i | ||
|
||
# Reconstruct the divisible subset | ||
result = [array[last_index]] | ||
while hash_array[last_index] != last_index: | ||
last_index = hash_array[last_index] | ||
result.append(array[last_index]) | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
|
||
array = [1, 16, 7, 8, 4] | ||
|
||
answer = largest_divisible_subset(array) | ||
|
||
print("The longest divisible subset elements are:", answer) | ||
pa-kh039 marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Uh oh!
There was an error while loading. Please reload this page.