Problem Statement :

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A, of size N, each memory location has some unique index, i (where 0<=i<N), that can be referenced as A[i] or Ai.

Reverse an array of integers.

Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.

Return [3,2,1].

Function Description:

Complete the function reverseArray in the editor below.

reverseArray has the following parameter(s):

 1. int A[n]: the array to reverse
 1 .int[n]: the reversed array

Input Format:

The first line contains an integer, N, the number of integers in A.
The second line contains N space-separated integers that make up A.

1<=N<=10^4 where A[i] is ith integer in A.

Solution :


                            Solution in C :

In C:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

   int n, new_number;
    scanf("%d", &n);
    int *p_numbers = (int*)malloc(n * sizeof(int));
    int i = 0;
    while (i < n) {
        scanf("%d", &new_number);
        *(p_numbers + i) = new_number;
    while (i >= 1) {
        printf("%d ", *(p_numbers + --i));
    return 0;

In C++:

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

int main() {
            int N;
            vector<int> arr(N);
            for (int i=0;i<N;i++)
                for (int i=0;i<N;i++)
                cout<<arr[i]<<" ";
            return 0;

In Java:

import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        int length = sc.nextInt();
        int[] array = new int[length];
        for (int i = 0; i < length ; i++) {
            array[i] = sc.nextInt(); 
        for (int i = 0; i < length; i++){
            System.out.print(array[length-i-1] + " ");

In Python 3:

n = int(input())
m = input().strip().split(' ')
res = m[::-1]

for i in range(n):
    r = r + str(res[i]) + " "

View More Similar Problems

Queue using Two Stacks

A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que

View Solution →

Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

View Solution →

Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

View Solution →

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →


This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →