Diagram Explanation with Time complexity: O(N) Auxiliary space: O(1) C++ Solution Easy to Understand
The Question is very badly explained but we can draw some sense by 2k.\n\nFirst we have to understand if it were only K instead of 2K.\n\nGiven an array arr[] o
The Question is very badly explained but we can draw some sense by **2k**.\n\nFirst we have to understand if it were only K instead of 2K.\n\nGiven an array arr[] of positive integers of size N. Reverse every sub-array group of size **K**.\nNote: 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).\n\n**Example** :\nN = 5, K = 3\narr[] = {1,2,3,4,5}\nOutput: 3 2 1 5 4\n\nExplanation: First group consists of elements 1, 2, 3. \nSecond group consists of 4,5.\nSo 1 2 3 -> Reverse -> 3 2 1 (satisfies K = 3)\n**Remaining is 4,5**\n4 - 5 -> Reverse -> 5 4 (Satisfies < K)\n**Final Reversal -> 3 2 1 5 4**\n\nWe Know if if the given string/array will be easy to replace if the size of given string/array is in multiple of K.\nExample :\nstring = "1 2 3 4 5 6" is of size 6 and is a multiple of k , where k = 3 . Then we can easily slide our range.\n1 2 3 reverse to 3 2 1 and 4 5 6 gets reverses to 6 5 4 without much of a concern of out of bounds error.\n\nWell here comes the trick part, if array/string is not a multiple of 3 then we will have to handle this situation.\n\nWatch illustration I made :\n# 1 STEP\n![image](https://assets.leetcode.com/users/images/0932f5c1-fa37-45b1-9bd9-3241a81341dc_1664049116.7054381.jpeg)\n # 2 STEP\n \n![image](https://assets.leetcode.com/users/images/82a73108-598c-4fda-a6ca-ca7572d51dde_1664049116.733301.jpeg)\n# 3 STEP\n\n\n![image](https://assets.leetcode.com/users/images/85df48e0-1528-4ced-93a3-81de04e3a962_1664049116.750314.jpeg)\n# 4 STEP\n\n![image](https://assets.leetcode.com/users/images/fbc7251e-7dab-4078-a25d-1b1981d2ed96_1664049116.6582966.jpeg)\n# 5 STEP\n\n![image](https://assets.leetcode.com/users/images/418fbbf6-59ee-4b8b-ad62-80c4421129f9_1664049116.7318428.jpeg)\n\n# 6 STEP\n\n\n![image](https://assets.leetcode.com/users/images/05aaddab-4186-49c5-a0f7-36f06237b6c0_1664049116.978711.jpeg)\n\n# 7 STEP\n\n![image](https://assets.leetcode.com/users/images/d0e22090-00d0-4e10-9f9e-104c984979fd_1664049116.9039936.jpeg)\n\n# 8 STEP\n\n![image](https://assets.leetcode.com/users/images/85d330b3-b728-48db-9e5b-37c971c68fb6_1664049116.905171.jpeg)\n\n# 9 STEP\n\n\n![image](https://assets.leetcode.com/users/images/5f839c0e-f0e3-4a84-8ee6-ab4ebe2d708f_1664049116.8595486.jpeg)\n\n# 10 STEP\n\n![image](https://assets.leetcode.com/users/images/50f191cc-4894-4fee-a93e-f394e830de31_1664049116.9855537.jpeg)\n\n# 11 STEP\n\n\n![image](https://assets.leetcode.com/users/images/00c39ada-e9c2-46dd-84d8-c3d6eed7ff45_1664049116.624134.jpeg)\n\n\nProcedure :\n\n**Traverse over the array and with each iteration:**\n* Set the left pointer as the current index and the right pointer at a distance of group size(K) \n* We will swap elements in the left and right indices, and increase left by one and decrease right by one\n* Do the above step until left < right\n* After the swap operation is done we will increment the value of the iterator by k ( group size )\n\nImplementation :\n```\n\tint n = s.size();\n    int k = 3;  //Hard Coded to test\n    int WindowStart = 0; //Starting of window\n    int WindowEnd = n - 1; //ending of window\n    for (int i = 0; i < n; i = i = i + (2 * k)) //2*K Because we are asked to reverse every 2K window\n    {\n        WindowStart = i;\n        WindowEnd = min((i + k - 1), n - 1);\n        while (WindowStart < WindowEnd)\n        {\n            swap(s[WindowStart], s[WindowEnd]);\n            WindowStart++;\n            WindowEnd--;\n        }\n    }\n```\n\nContributing first time! Any suggestions will be appreciated :)
['Two Pointers']