Hackerrank Arrays Introduction Solution

Hackerrank Arrays Introduction Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Declaration:

int arr[10]; //Declares an array named arr of size 10, i.e; you can store 10 integers.

Accessing elements of an array:

Indexing in arrays starts from 0.So the first element is stored at arr[0],the second element at arr[1]...arr[9]

You'll be given an array of  integers and you have to print the integers in the reverse order.

Input Format

The first line of the input contains ,where  is the number of integers.The next line contains  integers separated by a space.

Constraints

, where  is the  integer in the array.

Output Format

Print the  integers of the array in the reverse order in a single line separated by a space.

Sample Input

4
1 4 3 2

Sample Output

2 3 4 1

Solution in cpp

Approach 1.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int a[10009];
    int n; cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=n-1;i>=0;i--) cout<<a[i]<<" ";
    return 0;
}

Approach 2.

#include <iostream>

using namespace std;

int main()
{
    int N,total=0;
    cin >> N;
    
    int ka[N];
    for(int i=0;i<N;i++)
    {
        cin >> ka[i];
        //total++;
    }

    for(int i=N-1;i>=0;i--)
    {
        cout << ka[i] << " ";
    }

    return 0;
}

Approach 3.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int N;
    cin >> N;
    int arr[N];
    for(int i = 0; i < N; i++){
        cin >> arr[i];
    }
    for(int k = N-1; k > -1; k--){
        cout << arr[k] << " ";
    }
    return 0;
}

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe