All submissions

Reverse an Array In-Place

Code

#include <stdio.h>

void reverse_array(int a[], int n) {
    for(int i = n-1, j =0 ;i>j;i--,j++)
    {
        a[i] = a[i]+a[j];
        a[j] = a[i]-a[j];
        a[i] = a[i]-a[j];
    }
}

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

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

    reverse_array(a, n);

    for (int i = 0; i < n; i++) {
        printf("%d", a[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