C PROGRAM TO ROTATE THE SECOND HALF OF THE ARRAY TO THE LEFT FOR K NUMBER OF TIMES
Consider the following I/O samples.
SAMPLE INPUT: 5
1 2 3 4 5
5
SAMPLE OUTPUT: 1 2 5 3 4
The algorithm used here is :
- Input the values
- Store the half of the size of the array to a temporary variable.
- Swap the second half of the array for K times
- Print the output
C PROGRAM:#include<stdio.h>int main()
{
int arr[100],n,i,temp,rot;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&rot);
while(rot --)
{
temp = arr[n/];
for(i=n/2;i<n-1;i++)
arr[i]=arr[i+1];
arr[i] = temp;
}
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
OUTPUT:The given source code in C program "to rotate the second half of the array to the left for K number of times" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment