Left Rotate Array by K Positions

Code


#include <stdio.h>

void rotate_left(int *arr, int n, int k){
    int b[n];
    k = k%n;
    for(int i = 0; i < n; i++){
        b[i] = arr[(i+k)%n];
    }
    for(int i = 0; i< n; i++){
        printf("%d ", b[i]);
    }

}


int main(){ 
    int n, k, a[100];
    scanf("%d %d", &n, &k);
    
    for(int i = 0 ; i < n; i++){
        scanf("%d", &a[i]);
    }

    rotate_left( a, n, k);
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

5 2 1 2 3 4 5

Expected Output

3 4 5 1 2