Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Arrays – DS problem solution

YASH PAL, 31 July 2024

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.

coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes