Skip to content
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

Added another array problem named reverse_array_in_groups #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

krishnavamshidevandla
Copy link

Added another problem of DSA of array where you reverse sub-arrays in the main array according to given sub-array length

@krishnavamshidevandla krishnavamshidevandla changed the title Added another array problem names reverse_array_in_groups Added another array problem named reverse_array_in_groups Aug 4, 2023
@MTrajK
Copy link
Owner

MTrajK commented Aug 5, 2023

Okay, I have tried it and it looks like this:

def reverse_array_in_groups(arr, k):
    i = 0
    n = len(arr)
    group = 0
    group_size = k
    
    while i<n:
        if (i - group*k == k):
            group += 1
            if (n - i < k):
                group_size = n - i
            
        if (i - group*k < group_size//2):
            right = group*k + (group_size - 1 - (i - group*k))
            left = i
            
            print(i, arr[i], right)
            arr[left], arr[right] = arr[right], arr[left]
        
        i += 1
        
    return arr

But it can be simplified and refactored (some of the formulas can be simplified and some of the checks).

@krishnavamshidevandla could you please try it? thanks.

@MTrajK
Copy link
Owner

MTrajK commented Aug 5, 2023

You could also add both of the solutions in that file.

@krishnavamshidevandla
Copy link
Author

@MTrajK I have tried your solution, it's working fine. But I can't get my mind around it. Can you edit the solution with a detailed explanation using comments between the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants