HackerRank – 30 Days of Code – Day 7: Arrays – Ruby Program

Objective

Today, we’re learning 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.

Input Format

The first line contains an integer, N(the size of our array).
The second line contains N space-separated integers describing array A’s elements.

Constraints

  • 1 <= N <= 1000
  • 1 <- Ai <= 10000, where Ai 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

Ruby Implementation

#!/bin/ruby

n = gets.strip.to_i
arr = gets.strip
arr = arr.split(' ').map(&:to_i)

count = arr.size
op = ""
until (count == 0)    
    count -= 1
    op += arr[count].to_s + " "
end

puts op

Sourcehttps://www.hackerrank.com/challenges/30-arrays

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.