We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Arrays
- Left Rotation
- Discussions
Left Rotation
Left Rotation
+ 0 comments This problem is pretty similar to a Queue data struct. Since we dont have to "First-In" queue method, you can simply Dequeue or pop the first element of the array and "Enqueue" that popped element again at the last of the same array - Here is the simple Python Algorithm -
def rotateLeft(d, arr): for i in range(d): arr.append(arr.pop(0)) return arr
+ 0 comments CPP:
vector<int> rotateLeft(int d, vector<int> arr) { vector<int> tmp; // Copy elements from position d to the end of the array for (int i = d; i < arr.size(); i++) { tmp.push_back(arr[i]); } // Copy elements from the beginning to position d-1 for (int i = 0; i < d; i++) { tmp.push_back(arr[i]); } return tmp; }
+ 0 comments C , Time Complexity : O(n)
int* rotateLeft(int d, int arr_count, int* arr, int* result_count) { int *result = (int *)malloc(arr_count * sizeof(int)); int index = 0; for(int i=d ; i<arr_count; i++){ result[index] = arr[i] ; index++ ; } for(int i=0; i<d ; i++){ result[index] = arr[i] ; index++ ; } *result_count = arr_count ; return result ; }
+ 0 comments simple solution cpp vector rotateLeft(int d, vector arr) { int n = arr.size(); vector temp(n); for(int i =0;i
+ 0 comments C#
public static List<int> rotateLeft(int d, List<int> arr) { /// 1 2 3 4 5 6 7 8 9 10 /// 3 4 5 6 7 8 9 10 1 2 var holding = new int[d]; arr.CopyTo(0, holding, 0, d); arr.RemoveRange(0,d); arr.AddRange(holding); return arr; }
Load more conversations
Sort 3365 Discussions, By:
Please Login in order to post a comment