Quick Left Rotation | Practice | GeeksforGeeks

Given an array arr[] of size N and an integer K, the task is to left rotate the array K indexes
Example 1:
Input: N = 7, K = 2
arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 3 4 5 6 7 1 2
Explanation: Rotation of the above 
array by 2 will make the output array .

Example 2: 
Input: N = 6, K = 12
arr[] = {1, 2, 3, 4, 5, 6}
Output: 1 2 3 4 5 6


Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function leftRotate() that takes array arr, integer K and integer N as parameters and rotate the given array by d value.
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
1 ≤ N ≤ 105
 
0
mayank20211 day ago
C++ : 0.26/1.54void leftRotate(int arr[], int k, int n) {    k=k%n;      reverse(arr, arr+k);   reverse(arr+k, arr+n);   reverse(arr, arr+n);} 
0
arvind16yadav3 weeks ago
/*JAVA CODE (Time : 3 out of 4.7)*/
void leftRotate(long arr[], int k,int n)   {       int mod_k = k % n;       long[] brr = new long[mod_k];       for(int i=0; i<mod_k; i++){           brr[i] = arr[i];       }       for(int i=0; i<n-mod_k; i++){           arr[i] = arr[i+mod_k];       }       for(int i=0; i<mod_k; i++){           arr[n-mod_k+i] = brr[i];       }   }
0
codewithshoaib191 month ago
 public static void leftRotate(long arr[], int k,int n)   {        if (k < 0) {           k = k + n;       }       k = k % n;       reverse(arr, 0, k - 1);       reverse(arr, k, n - 1);       reverse(arr, 0, n - 1);   }   public static void reverse(long[] arr, int s, int e) {       while (s <= e) {           swap(arr, s, e);           s++;           e--;       }   }
   public static void swap(long[] arr, int s, int e) {       long temp = arr[s];       arr[s] = arr[e];       arr[e] = temp;   }
0
pankajkumarravi6 months ago
static void leftRotate(long arr[], int k, int n) {    long temp[] = new long[n];    int mod = k % n;    for(int i = 0 ; i < n; i++){        temp[i] = arr[(i+mod) % n];    }    for(int i = 0 ; i < n ; i++){        arr[i]= temp[i];    }}
0
raj2000ath7 months ago
	error
	: not compiling.
	   
	         A = []
       B = []
       for i in range (0,k):
           A.append(arr[i])
       for i in range (k,n):
           B.append(arr[i])
       A.reverse()
       B.reverse()
       list(A)
       list(B)
       for i in range(0,len(B)):
           A.append(B[i])
       arr.clear()
       arr.append(A)
       reversed(arr)
0
raj2000ath7 months ago
Why is this not compiling:??
 
class Solution:   def leftRotate(self, arr, k, n):       # Your code goes here       arr11 = []       i = 0       ri = k       while ri < n:           arr11[i] = arr[ri]           i += 1           ri += 1       ri = 0       while ri < k:           arr11[i] = arr[ri]           i += 1           ri += 1       return arr11
 
 
 
 
-1
Chandan kumar Tripathi9 months ago
Chandan kumar Tripathi
void reverse(int arr[] , int low , int high)    {        while(low<high) {="" swap(arr[high]="" ,="" arr[low]);="" low++;="" high--;="" }="" }="" void="" leftrotate(int="" arr[],="" int="" k,="" int="" n)="" {="" your="" code="" goes="" here="" k="k%n;" reverse(arr="" ,="" 0="" ,="" k-1);="" reverse(arr="" ,k="" ,="" n-1);="" reverse(arr="" ,="" 0="" ,="" n-1);="" }<="" code="">
-1
Chandan kumar Tripathi9 months ago
Chandan kumar Tripathi
void reverse(int arr[] , int low , int high)    {        while(low<high) {="" swap(arr[high]="" ,="" arr[low]);="" low++;="" high--;="" }="" }="" void="" leftrotate(int="" arr[],="" int="" k,="" int="" n)="" {="" your="" code="" goes="" here="" k="k%n;" reverse(arr="" ,="" 0="" ,="" k-1);="" reverse(arr="" ,k="" ,="" n-1);="" reverse(arr="" ,="" 0="" ,="" n-1);="" }="">
-1
Chandan kumar Tripathi9 months ago
Chandan kumar Tripathi
void reverse(int arr[] , int low , int high)    {        while(low<high) {="" swap(arr[high]="" ,="" arr[low]);="" low++;="" high--;="" }="" }="" void="" leftrotate(int="" arr[],="" int="" k,="" int="" n)="" {="" your="" code="" goes="" here="" k="k%n;" reverse(arr="" ,="" 0="" ,="" k-1);="" reverse(arr="" ,k="" ,="" n-1);="" reverse(arr="" ,="" 0="" ,="" n-1);="" }<="" i="">
+1
Debojyoti Sinha9 months ago
Debojyoti Sinha
Problem Solved Successfully ✅Total Time Taken: 0.3/1.5
class Solution{    public:    void leftRotate(int arr[], int K, int N)     {        rotate(arr, arr + (K % N), arr + N);    }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.