Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
573 B
3
Indexable
int array_left(int* arr,int size,int steps){
    int *temp = (int *)malloc(size * sizeof(int));
    if (temp == NULL) {
        printf("Memory allocation failed.\n");
        return -1;
    }
    // Initialize all elements to zero
    for (int i = 0; i <size; i++) {
        temp[i] = arr[i];
    }
    int i;
    int new_index;
    for(i=0;i<size;i++){
        if((i-steps)>=0){
            new_index=i-steps;
        }
        else{
            new_index=i-steps+size;
        }
        arr[new_index]=temp[i];
    }
    free(temp);
    return 1;
}