In this HackerRank Arrays - DS problem, we need to develop a program that can take an integer array as input and then reverse it. also, we need to make a reveseArray function that can return the reverse array. For example if we give input  arr = [2,3,5] then it must return [5,3,2].

hackerrank Arrays - DS problem solution

Problem solution in Python programming.

length = input()
array = input()
array = array.split(' ')
array = [int(x) for x in array if x != '']
array = array[::-1]
for x in array:
    print(x, end=' ')
print('')


Explanation

Here we first take the input of length of array and the array itself. then we split its values and then we restore the values of array within it and then we reverse the array and store it in itself and then we print its values on the output screen.

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] input = new int[count];
        
        for(int i = 0; i < count; i++){
            input[i] = sc.nextInt();
        }
        for(int i = count-1; i >= 0; i--){
            System.out.print(input[i] + " "); 
        }
        
    }
}


Explanation

Here in the above code we first take the inputs one is length of array and then values of array using the for loop and store them. and then we print it in reverse order using the for loop. here we just print the array from the last element as first element.

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int a[100000];


int main() {
    int n, i;
    scanf ("%d", &n);
    for (i = 0;i < n; i ++)
        scanf ("%d", &a[i]);
    for (i = n - 1; i >= 0; i --)
        printf ("%d ", a[i]);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}


Explanation

Here as usual we first declare two inputs and then we take the inputs one integer input that is length of array and then the values of array using the for loop. after that we print the value of array from last to first in reverse order.

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    int n;
    scanf("%d",&n);
    int i = 0;
    int array[1000];
    for (i;i<n;i++)
        {
        scanf("%d ",&array[i]);
    }
    i = n-1;
    for (i;i>-1;i--)
        {
        printf("%d ", array[i]);
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}


Explanation

Here in the above code we used an fixed array length of 1000 because the input value is length can't be more than 1000. after that we store the input values and then print the array in the reverse order.