Array Left Rotation Algorithm

The Array Left Rotation Algorithm is a popular programming technique used to shift the elements of an array towards the left by a specified number of positions, effectively rotating the elements around the array. This algorithm is particularly useful in various applications such as data manipulation, computer graphics, and cryptography. The primary goal of this algorithm is to reposition the elements of an array such that each element moves to its new location by a fixed number of positions, wrapping around the array when necessary. For example, if we have an array of elements [1, 2, 3, 4, 5] and we want to perform a left rotation by two positions, the resulting array would be [3, 4, 5, 1, 2]. There are several approaches to implementing the Array Left Rotation Algorithm, but the most common methods involve using loops, temporary arrays, or in-place rotation through a series of swaps or reversals. In the case of using loops, the algorithm iterates through the elements of the array and assigns the values to their new positions, while using a temporary array allows for a more space-efficient solution by storing the displaced elements before reassigning them to their new positions in the original array. Alternatively, the in-place rotation approach avoids the need for extra storage by performing a series of swaps or reversals within the array itself, effectively rotating the elements one by one until the desired rotation is achieved. Regardless of the implementation, the Array Left Rotation Algorithm provides a powerful and flexible means of performing element rotations in an array, making it a valuable tool for programmers and developers alike.
#include <iostream>
using namespace std;
int main()
{
	int n, k;
	cout << "Enter size of array=\t";
	cin >> n;
	cout << "Enter Number of indeces u want to rotate the array to left=\t";
	cin >> k;
	int a[n];
	cout << "Enter  elements of array=\t";
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	int temp = 0;
	for (int i = 0; i < k; i++)
	{
		temp = a[0];
		for (int j = 0; j < n; j++)
		{
			if (j == n - 1)
			{
				a[n - 1] = temp;
			}
			else
			{
				a[j] = a[j + 1];
			}
		}
	}
	cout << "Your rotated array is=\t";
	for (int j = 0; j < n; j++)
	{
		cout << a[j] << " ";
	}
	getchar();
	return 0;
}

LANGUAGE:

DARK MODE: