Print left rotation of array in O ( n ) time and O ( 1 ) space | C ++ Implementation For Print Left Rotation Of Any Array K Times ; Function For The k Times Left Rotation ; Stl function rotates takes three parameters - the beginning , the position by which it should be rotated , the end address of the array The below function will be rotating the array left in linear time ( k % arraySize ) times ; Print the rotated array from start position ; Driver program ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void leftRotate ( int arr [ ] , int k , int n ) { rotate ( arr , arr + ( k % n ) , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; leftRotate ( arr , k , n ) ; return 0 ; }