All submissions

Reverse an Array In-Place

Code

#include <stdio.h>
#include <stdint.h>

void reverse_array(int arr[], int n) {
    // Your logic here
    int *strt_idx = &arr[0];
    int *last_idx = &arr[n-1];
    uint8_t temp = 0;
    for(uint8_t it=0; it<n/2; it++)
    {
        temp = *strt_idx;
        *strt_idx = *last_idx;
        *last_idx = temp;
        strt_idx++;
        last_idx--;
    }
}

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

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

    reverse_array(arr, n);

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

Solving Approach

 

 

 

Loading...

Input

5 1 2 3 4 5

Expected Output

5 4 3 2 1