Left shift in array in C++ by R4R Team

Array:
Array is the collection of different values having same data type.
-We have a integer Array that having all integer values.
-Character array is known as String.

Left Shift in Array:
Example-
Array is {1,2,3,4,5}
After Left Shift {2,3,4,5,1}

program:

#include < iostream>
using namespace std;
int main()
{
int a[20],b[20],i,n;
cout<<"Number of element in array?"<< endl;
cin>>n;
cout<<"Enter element in array"<< endl;
//input in first array
for(i=0;i< n;i++){
cin>>a[i];
}
//now perform left shift
for(int i=0;i< n;i++)
{
b[i]=a[(1+i)%n];
}
//Now display second array
cout<<"After Left shift array is: "<< endl;
for(i=0;i< n;i++)
{
cout<< b[i]<<" ";
}
return 0;
}


output-

Number of element in array?
5
Enter element in array
1 2 3 4 5
After Left shift array is:
2 3 4 5 1

-In this program, we take a input of n numbers in Array.
-then we do the left shift by the b[i]=a[(1+i)%n];
run for every value of i.
-Left shift array is store in another Array then we print that.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!