An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index,  (where ), that can be referenced as  (you may also see it written as ).
Given an array, , of  integers, print each element in reverse order as a single line of space-separated integers.

Question from hackerrank.

Solution in C# -


class Solution {
    static int[] reverseArray(int[] a) {
        Array.Reverse(a);
        return a;
    }
    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
        int arrCount = Convert.ToInt32(Console.ReadLine());
        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt32(arrTemp)) ;
        int[] res = reverseArray(arr);
   textWriter.WriteLine(string.Join(" ", res));
     textWriter.Flush();
      textWriter.Close();
    }
}

Post a Comment

Previous Post Next Post