Left Rotate Array by K Positions

Code

#include <stdio.h>

void rotate_array(int arr[],int n)
{
    int start =0;
    int end =n-1;
    int temp=0;
    while(end>start)
    {   temp=arr[start];
        arr[start]=arr[end];
        arr[end]=temp;
        start++;
        end--;
    }
}
void rotate_left(int arr[], int n, int k) {
    // Your logic here
int k1=k%n;// for wrap around condt
rotate_array(arr,k1);//rotate upto k
rotate_array(arr,n);//rotate the whole 
rotate_array(arr,n-k1);

}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    int arr[100];

    // Read array elements
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Rotate the array
    rotate_left(arr, n, k);

    // Print the rotated array
    for (int i = 0; i < n; i++) {
        printf("%d", arr[i]);
        if(i < n-1){
        	printf(" ");
        }
    }

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5 2 1 2 3 4 5

Expected Output

3 4 5 1 2