Array problem solution - 30 days of code HackerRank

Input Format
The first line contains an integer,  (the size of our array).
The second line contains  space-separated integers describing array 's elements.
Output Format
Print the elements of array  in reverse order as a single line of space-separated numbers.
Array problem solution - 30 days of code HackerRank

Problem solution in Java programming language.

import java.io.*;
 import java.util.*;
 import java.text.*;
 import java.math.*;
 import java.util.regex.*;

 public class Solution {

     public static void main(String[] args) {
         // Get the input
         Scanner sc = new Scanner(System.in);
         int length = sc.nextInt();
         int[] array = new int[length];
        
         for (int i = 0; i < length; i++) {
             array[i] = sc.nextInt();
         }
        
         // Var holding our new string
         String result = "";
        
         for (int i = array.length - 1; i >= 0; i--) {
             result = result + array[i] + " ";
         }
        
         System.out.println(result);
     }
 }

Second solution.

public class Solution {

    public static void print(String[] arr){
        for(int i = arr.length - 1; i >= 0; i--){
            System.out.print(arr[i] + " ");
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        String[] arr = in.nextLine().split(" ");
        in.close();
        print(arr);
    }
}

Problem solution in the python programming language.

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    n = int(input())

    arr = list(map(intinput().rstrip().split()))

    for i in reversed(arr):
        print(i,end=' ')

Post a Comment

0 Comments