Here you learn Program for the left turn of an array in C ++, this problem you see in competitive programming sites like leetcode, hackerrank etc. You have an array and then rotate the array clockwise.
A left turn of an array or an array rotation in one in the left direction are both identical. By doing this, we first store the first element of an array in a temporary space and then move the array components one by one on the left side of the array and at the end we will replace the last element of the array with a temporary element.
#include<bits/stdc++.h> using namespace std; int main() int arr = 11, 22, 33, 44, 55; //Initialization array int len = sizeof(arr)/sizeof(arr); //Calculate length of array arr cout<<"Array Before Left Rotation: n"; //Displays Array for (int i = 0; i < len; i++) cout<<arr[i]<<"t"; int j, temp; temp = arr; //Stores the first element of the array into temporary space for(j = 0; j < len-1; j++) arr[j] = arr[j+1]; //Shift element of array by one arr[j] = temp; //Now Set temporary element of array to the end cout<<"n"; cout<<"Array After Left Rotation: n"; //Displays Resulting Array for(int i = 0; i < len; i++) cout<<arr[i]<<"t"; return 0;
Array Before Left Rotation: 11 22 33 44 55 Array After Left Rotation: 22 33 44 55 11
I hope you enjoy this example if you have a doubt leave it in the comments box below.
Happy Coding 😊