Array Left Rotation Hackerrank problem solution- CoderInMe

preview_player
Показать описание
Do you know how to rotate an array by left or right rotation..??!! Let me help you with a few discussion on Hackerrank left array rotation problem by using simple steps. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].
Рекомендации по теме
Комментарии
Автор

def rotLeft(array, d):
s = d% len(array)

# uncomment this line to reverse the shift direction
# s *= -1

# shift array with list slicing
shifted_array = array[s:] + array[:s]

return shifted_array

maskahleo
Автор

Can you please implement in the c: The Array Left Rotation problem:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int* leftRotation(int a_size, int* a, int d, int *result_size) {
//implement the code here
//I didn't get what to do
}

int main() {
int n;
int d;
scanf("%i %i", &n, &d);
int *a = malloc(sizeof(int) * n);
for (int a_i = 0; a_i < n; a_i++) {
scanf("%i", &a[a_i]);
}
int result_size;
int* result = leftRotation( a, d, &result_size);
for(int result_i = 0; result_i < result_size; result_i++) {
if(result_i) {
printf(" ");
}
printf("%d", result[result_i]);
}
puts("");


return 0;
}

sagarrana