Reverse array queries hackerrank solution JavaScript



Array Reversal



Given an array, of size n, reverse it. Example: If array, arr=[1.2.3.4,5] after reversing it, the array should be, arr=[5,4,3,2,1] Input Format: The first line contains an integer, n, denoting the size of the array. The next line contains n space-separated integers denoting the elements of the array. Constraints: 1<=n<=1000 1<=arr[i]<=1000, where is the element of the array. Output Format: The output is handled by the code given in the editor, which would print the array.



#include <stdio.h> #include <stdlib.h> 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); } for(i = num-1; i>-1; i--) printf("%d ", *(arr + i)); return 0; }


Am I leaning in the right direction with my code? I'm currently working on hacker rank and am on the easy section of data structures yet I still am confused about how to reverse this array!

Out of all my attempts, I like these two that I did. Check it out.

one attempt:

Reverse array queries hackerrank solution JavaScript

two attempts:

Reverse array queries hackerrank solution JavaScript

5

Reverse Array Queries - Python HackerRank Solutions
For a given array of integers, perform operations on the array. Return the resulting array after all operations have been applied in the order given. Each operation contains two indices. Reverse the subarray between those zero-based indices, inclusive.

To view the content, you need to Sign In .

Solutions-

To view the content, you need to Sign In .

Reverse Array Queries - Python HackerRank Solutions

Last edited: Mar 10, 2022

Reverse array queries hackerrank solution JavaScript

Staff member Administrator

  • Messages 224
  • Reaction score 502
  • Points 93

Show hidden low quality content

You must log in or register to reply here.