Algorithm
Problem Name: Data Structures -
In this HackerRank in Data Structures -
An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A , of size N , each memory location has some unique index, i (where 0 <= i < N), that can be referenced as A[i] or Ai.
Reverse an array of integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Example
A = [1,2,3]
Return [3,2,1]
Function Description
Complete the function reverseArray in the editor below.
reverseArray has 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 <= 10**3
- 1 <= A[i] <= 10**4 , where A[i] is the i**th integer in A
Sample Input 1
Code Examples
#1 Code Example with C Programming
Code -
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;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C++ Programming
Code -
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;
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
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] + " ");
}
}
}
Copy The Code &
Try With Live Editor
#5 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
int n = Convert.ToInt32(Console.ReadLine());
string[] arr_temp = Console.ReadLine().Split(' ');
int[] arr = Array.ConvertAll(arr_temp.Take(n).ToArray(), Int32.Parse);
var reverse = arr.Reverse();
foreach (var item in reverse)
{
Console.Write(item + " ");
}
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
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('')
Copy The Code &
Try With Live Editor
#6 Code Example with Javascript Programming
Code -
Javascript Programming
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'reverseArray' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY a as parameter.
*/
function reverseArray(a) {
// Declare an array with numbers 1-10
var n = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// Add specifications to the problem
1 < = n <= 1000;
// Reverse the array element's order
a.reverse();
// Return the reversed array
return a;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const arrCount = parseInt(readLine().trim(), 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const res = reverseArray(arr);
ws.write(res.join(' ') + '\n');
ws.end();
}
Copy The Code &
Try With Live Editor