# C++ program to print the left Rotation of the array

Left rotation of the array using C++ program: Here, we are going to learn how to print the array elements after left rotation?
Submitted by Vivek Kothari, on February 13, 2019

Problem statement:

Given an array of N elements and the task is to print the elements of an array after left rotating array elements by d positions.

Input: N, d and next line containing the n elements of array.

Output: Array elements after d rotation.

Example:

```    Input:
n = 7, d = 2
array elements: 1 2 3 4 5 6 7

Output:
3 4 5 6 7 1 2
```

Solution:

The naïve approach to solve this problem is to shift the all elements d times but this is a time-consuming process.

We can use a small trick here to print the elements of the array after left rotating d elements.

```    Let,
i = ith iteration
D = number of elements to rotate
N = size of array
Then to left rotate array we can use -
arr[i] = arr[(i+D)%N]
```

C++ Implementation:

```#include <iostream>
using namespace std;

int main()
{
int n,d;

//input value of n and d
cout<<"Enter the value of n and d"<<endl;
cin>>n>>d;
int a[n];

//input array elements
cout<<"enter the array elements : ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}

//print the elements of array after rotation
cout<<"array elements after rotation : ";
for(int i=0;i<n;i++)
{
cout<<a[(i+d)%n]<<" ";
}

return 0;
}
```

Output

```Enter the value of n and d
7 3
enter the array elements : 1 2 3 4 5 6 7
array elements after rotation : 4 5 6 7 1 2 3
```

TOP Interview Coding Problems/Challenges

Learn PCB Designing: PCB DESIGNING TUTORIAL

 Recommended posts C Tips & Tricks, C++ Tips & Tricks Introduction to Linux (Its modes, Safety, Most popular Applications) Linux Best Distros of 2018 C programming optimization techniques Differences b/w C & Embedded C? Embedded C Interview Q. & A. C programming tips for Embedded Development Basic rules of writing a C program Important points (rules) to remember while writing C/C++ program Top 5 Websites for solving programming challenges Read more...

 Others... Computer G.K. (MCQ) Most viewed pages... Categories...

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates