Algorithm


Problem Name: 30 days of code - Day 7: Arrays

Problem Link: https://www.hackerrank.com/challenges/30-arrays/problem?isFullScreen=true

In this HackerRank in 30 Days of Code - Day 7: Arrays problem solution,

Objective
Today, we will learn about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video.

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  space-separated integers that describe array A's elements.

Constraints

  • 1 <= N <= 1000
  • 1 <= A[i] <= 10000,where ai is the 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

 

 

 

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n,i;
    scanf("%d",&n);
    int A[n];
    for(i=0;i < n;i++)
    scanf("%d",&A[i]);


    for(i=n-1;i>=0;i--)
    printf("%d ",A[i]);


}

Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <algorithm>
#include <iostream>

using namespace std;

int main() {
    int N;
    cin >> N;

    int arr[N];

    for (int i = 0; i  <  N; i++) {
        cin >> arr[i];
    }

    reverse(arr, arr + N);

    for (int i = 0; i  <  N; i++) {
        cout << arr[i] << " ";
    }

    return 0;
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


using System;

class Solution
{
    static void Main(String[] args)
    {
        Console.ReadLine();

        var str = Console.ReadLine();
        var arr = str.Split(' ');

        Array.Reverse(arr);

        foreach (var num in arr)
        {
            Console.Write($"{num} ");
        }
    }
}
Copy The Code & Try With Live Editor

#4 Code Example with Golang Programming

Code - Golang Programming

<
package main

import "fmt"

func printReverse(l, h int, arr []int) {
	if l > h {
		return
	} else {
		printReverse(l+1, h, arr)
		fmt.Printf("%d ", arr[l])
	}
}

func main() {
	var n int

	fmt.Scan(&n)

	a := make([]int, n)
	for k := range a {
		fmt.Scan(&a[k])
	}
	printReverse(0, n-1, a)
}
Copy The Code & Try With Live Editor

#5 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

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();
        }

        for (int i = 0; i  <  n; i++) {
            int idx = n - i - 1;
            System.out.print(arr[idx] + " ");
        }

        in.close();
    }
}
Copy The Code & Try With Live Editor

#6 Code Example with Python Programming

Code - Python Programming


input()

arr = str(input()).split(" ")
arr.reverse()

for num in arr:
    print(num + " ", end="")
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Day 6: Let's Review solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code
Next
[Solved] Day 8: Dictionaries and Maps solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code