Sort by

recency

|

847 Discussions

|

  • + 0 comments

    Saw a lot of code that prints the array just in reverse which is a perfect solution for this problem. If you want to actually reverse the array in your code and use it further I implemented the reversal with a xor swap. This way you don't need a temporary array or variable.

    include

    include

    int main() { int num, arr, i; scanf("%d", &num); arr = (int) malloc(num * sizeof(int)); for(i = 0; i < num; i++) { scanf("%d", arr + i); }

    /* Write the logic to reverse the array. */
    
    // reverse array using xor swap
    for(i = 0; i < num / 2; i++) {
        // implement xor swap
        // a, b
        // a = a^b
        // b = a^b
        // a = a^b
        // a and b values are swapped
        arr[i] = arr[i] ^ arr[num - i - 1];
        arr[num - i - 1] = arr[i] ^ arr[num - i - 1];
        arr[i] = arr[i] ^ arr[num - i - 1];
        }
    
    
    for(i = 0; i < num; i++)
        printf("%d ", *(arr + i));
    
    free(arr);
    
    return 0;
    

    }

  • + 0 comments

    finally done something on my own `

    include

    include

    int main() { int i, j, num; scanf("%d", &num);

    // Dynamic memory allocation
    int *arr = (int *)malloc(num * sizeof(int));
    
    for(i = 0; i < num; i++) {
        scanf("%d", &arr[i]);
    }
    
    for(j = num - 1; j >= 0; j--) {
        printf("%d ", arr[j]);
    }
    
    free(arr);
    
    return 0;
    

    }

  • + 0 comments

    Here is Array Reversal in c problem solution - https://programmingoneonone.com/hackerrank-array-reversal-solution-in-c.html

  • + 0 comments
    int main()
    {
        int num, *arr, i;
        scanf("%d", &num);
        arr = (int*) malloc(num * sizeof(int));
        for(i = 0; i < num; i++) {
            scanf("%d", arr + i);
        }
    
    
        /* Write the logic to reverse the array. */
        int temp;
        for(i=num-1;i>=num/2;i--){
            temp = *(arr+((num-1)-i));
            *(arr+((num-1)-i)) = *(arr+i);
            *(arr + i) = temp;
        }
        
        for(i = 0; i < num; i++)
            printf("%d ", *(arr + i));
        return 0;
    }
    
  • + 0 comments

    include

    include

    int main(){

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

    }