Skip to content
the poor coder

Hackerrank Arrays Introduction Solution

Poor Man
Published
Updated
Reading time
2 min read

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:

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

Accessing elements of an array:

Text
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

Text
4
1 4 3 2

Sample Output

Text
2 3 4 1

Solution in cpp#

Approach 1.

Python
#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.

Python
#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.

Python
#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;
}

Keep going

2020

Hackerrank Pointer Solution

A pointer in C is a way to share a memory address among different contexts (primarily functions). They are primarily used whenever a function needs to modify the content of a variable, of which it doesn't have…

2020

Hackerrank Functions Solution

Functions are a bunch of statements glued together. A function is provided with zero or more arguments, and it executes the statements on it. Based on the return type, it either returns nothing (void) or something. A…

ESC