![]() |
Given an array of integers and an integer K, rotate the array to the right by K positions in a circular manner. Examples: Input: nums = [1, 2, 3, 4, 5], k=2 Below are the approaches to finding the Circular rotation of an array by K positions which are as follows: Table of Content Using Extra SpaceWe first copy the last K elements of the array into a separate array and then shift the remaining elements of the original array to the right by K positions and in the last we copy the rotated elements back into the original array. Example: Implementation of program to Find Circular rotation of an array by K positions using Extra Space
Output Original Array: [ 1, 2, 3, 4, 5 ] Circularly rotated Array: [ 4, 5, 1, 2, 3 ] Time Complexity: O(n), where ????n is the number of elements in the array. Auxiliary Space: O(k), where K is the number of elements to rotate. Reverse TechniqueIn this approach, we use the reverse technique to rotate the array circularly by k positions to the right. It first reverses the entire array, then reverses the first k elements, and finally reverses the remaining elements. Example: Implementation of program to Find Circular rotation of an array by K positions using Reverse Technique.
Output Original Array: [ 1, 2, 3, 4, 5 ] Circularly rotated Array: [ 4, 5, 1, 2, 3 ] Time Complexity: O(N),where ????n is the number of elements in the array Auxiliary Space: O(1) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |