-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,35 @@ | ||
Given an array arr[] of positive integers of size N. Reverse every sub-array group of size K. | ||
|
||
Note: If at any instance, there are no more subarrays of size greater than or equal to K, then reverse the last subarray (irrespective of its size). You shouldn't return any array, modify the given array in-place. | ||
|
||
Example 1: | ||
|
||
Input: | ||
N = 5, K = 3 | ||
arr[] = {1,2,3,4,5} | ||
Output: 3 2 1 5 4 | ||
Explanation: First group consists of elements | ||
1, 2, 3. Second group consists of 4,5. | ||
|
||
|
||
Example 2: | ||
|
||
Input: | ||
N = 4, K = 3 | ||
arr[] = {5,6,8,9} | ||
Output: 8 6 5 9 | ||
|
||
|
||
Your Task: | ||
You don't need to read input or print anything. The task is to complete the function reverseInGroups() which takes the array, N and K as input parameters and modifies the array in-place. | ||
|
||
|
||
|
||
Expected Time Complexity: O(N) | ||
Expected Auxiliary Space: O(1) | ||
|
||
|
||
|
||
Constraints: | ||
1 ≤ N, K ≤ 107 | ||
1 ≤ A[i] ≤ 1018 |
This file contains 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,21 @@ | ||
#User function template for Python | ||
|
||
class Solution: | ||
#Function to reverse every sub-array group of size k. | ||
def reverseInGroups(self, arr, N, K): | ||
print("ana are mere") | ||
for x in arr: | ||
print(x) | ||
# code here | ||
|
||
import math | ||
def main(): | ||
N = 5 | ||
K = 3 | ||
arr = {1,2,3,4,5} | ||
ob = Solution() | ||
ob.reverseInGroups(arr,N,K) | ||
|
||
if __name__=="__main__": | ||
main() | ||
|