-
Notifications
You must be signed in to change notification settings - Fork 625
Added another array problem named reverse_array_in_groups #32
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
Conversation
Added another problem in the Arrays category
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.
You could also add both of the solutions in that file.
@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.
Added another problem of DSA of array where you reverse sub-arrays in the main array according to given sub-array length