Hello coders, today we are going to solve Day 7: Arrays HackerRank Solution in C++, Java and Python.
Objective
Today, we will learn about the Array data structure.
Task
Given an array, A, of N integers, print A‘s elements in reverse order as a single line of space-separated numbers.
Example
A = [1 ,2, 3, 4]
Print 4 3 2 1
. Each integer is separated by one space.
Input Format
The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers that describe array A‘s elements.
Constraints
- 1 <= N <= 1000
- 1 <= A[i] <= 10000, where A[i] is the ith integer in the array.
Output Format
Print the elements of array A in reverse order as a single line of space-separated numbers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
Solution – Day 7: Arrays
C++
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; /* * * Prosen Ghosh * American International University - Bangladesh (AIUB) * */ int main(){ int n; cin >> n; vector<int> arr(n); for(int arr_i = 0;arr_i < n;arr_i++){ cin >> arr[arr_i]; } for(int arr_i = n-1;arr_i >= 0;arr_i--){ cout << arr[arr_i] << " "; } cout << endl; return 0; }
Java
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for(int i=0; i < n; i++){ arr[i] = in.nextInt(); } in.close(); for(int i=n-1; i >=0; i--) { System.out.print(arr[i] + " "); } } }
Python
#!/bin/python3 import math import os import random import re import sys if __name__ == '__main__': n = int(input()) arr = list(map(int, input().rstrip().split())) print (*arr[::-1], sep=' ')
Disclaimer: The above Problem (Day 7: Arrays) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.