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 :

    1. Input the values
    2. Store the half of the size of the array to a temporary variable.
    3. Swap the second half of the array for K times
    4. 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

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.