Skip to content
thecscience
THECSICENCE

Learn everything about computer science

  • Home
  • Human values
  • NCERT Solutions
  • HackerRank solutions
    • HackerRank Algorithms problems solutions
    • HackerRank C solutions
    • HackerRank C++ solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
thecscience
THECSICENCE

Learn everything about computer science

Hackerrank Array – DS Problem Solution

Yashwant Parihar, March 17, 2025March 18, 2025

In this post, we will solve the Array – Ds Problem in HackerRank. An array is a data structure that stores elements of the same type in a contiguous memory block. In an array. A. of size N, each memory location
has some unique index, (where 0 < i < N). that can be referenced as A[i] or Ai.
Your task is to reverse an array of integers.
Note: You may want to skip this if you’ve already solved our C++ domain’s Arrays Introduction challenge.

Example
A = [1, 2, 3]
Return [3, 2, 1].

Function Description
Complete the function reverse Array with the following parameter(s):

  • int A[n]: the array to reverse

Returns

  • int[n]: the reversed array


Input Format
The first line contains an integer, N. The number of integers in A.
The second line contains N space-separated integers that make up A.

Constraints
1 ≤ N ≤ 103
1 ≤ A[i] < 104, where A[i] is the ith integer in A

Array – DS Problem Solution in C

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

int main() {

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

Array – DS Problem Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() 
{
    
    int cnt;
    scanf("%d", &cnt);
    
    int* numArray;
    
    numArray = new int[cnt];
    
    for(int k = 0 ; k < cnt ; k++)
    {
        scanf("%d", numArray+k);        
    }
    
    for(int k = cnt-1 ; k >= 0 ; k--)
        printf("%d ", numArray[k]);
    
    
}

Array – DS Problem Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

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

Array – DS Problem Solution in JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var n = parseInt(readLine());
    arr = readLine().split(' ');
    arr = arr.map(Number);
    console.log(arr.reverse().join(' '))
}

Array – DS Problem Solution in Python

#!/bin/python

import sys


n = int(raw_input().strip())
arr = map(int,raw_input().strip().split(' '))
result = []

for i in xrange(len(arr)):
    print str(arr[len(arr) - i - 1]),
    

More Solutions:-

HackerRank 2D Array – DS Problem Solution
HackerRank Dynamic Array Problem Solution

HackerRank Solutions Hackerrank Solutions

Post navigation

Previous post
Next post
  • HackerRank Dynamic Array Problem Solution
  • HackerRank 2D Array – DS Problem Solution
  • Hackerrank Array – DS Problem Solution
  • Von Neumann and Harvard Machine Architecture
  • Development of Computers
©2025 THECSICENCE | WordPress Theme by SuperbThemes