PRINT21-Editorial

PROBLEM LINK:

Practice

Author: RAKSHIT NAYAK

Tester: RAKSHIT NAYAK

Editorialist: RAKSHIT NAYAK

DIFFICULTY:

EASY

PREREQUISITES:

Arrays

PROBLEM:

The problem is that we are given a array, we have to print the reverse of the array. For detailed problem statement you can visit the link mentioned here ā†’ Practice

EXPLANATION:

This algorithm in real does not produces a reversed array. Instead it just prints array in reverse order. So here goes step by step descriptive logic to print array in reverse order,

  • Input size and elements in array from user. Store it in some variable say size and arr.
  • Run a loop from size -1 to 0 in decremented style. The loop structure should look like for(i=size-1; i>=0; iā€“).
  • Inside loop print current array element i.e. arr[i].

Time Complexity

Time complexity is O(n)

SOLUTION:

Setter's Solution
C ++ code:
#include <iostream>
using namespace std;
main()
{
    int n, i;
    cin >> n;
    int arr[n];
    for (i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
 
    for (i = n - 1; i >= 0; i--)
    {
 
        cout << arr[i] << " ";
    }
    return 0;
}

Well this was my approach ,feel free to share your approach here. Suggestions are always welcomed. :smile: