HackerRank Day 7 Solution in Python: Arrays

Today we will see the HackerRank Day 7 Solution in Python. The problem is named Arrays which is part of 30 Days of code on HackerRank. Let’s get started!

Day 7: Arrays Problem statement

We are given an array of n integers, our task is to print the array elements in reverse order as a single line of space-separated numbers.

Sample Input

4
1 4 3 2

Sample Output

2 3 4 1

You can solve the problem here.

HackerRank Day 7 Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    #Number of array elements
    n = int(input().strip())
    
    #Get Array elements
    arr = list(map(int, input().rstrip().split()))
    # reverse the array with list slicing
    rev_arr = arr[::-1]  
    #Print the reversed array
    for i in rev_arr:
        print(i, end=" ")

Explanation

  • First, we get the array elements as the input
  • Then, we use list slicing to reverse the array and store the reversed array in a new array
  • Then, we print the reversed array elements

Also Read:

Share:

Author: Keerthana Buvaneshwaran