Arrays – DS

Task:

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.

Example
A = [1,2,3];
Return A.

Function Description

Complete the function reverseArray in the editor below.

reverseArray has the following parameter(s):

  • int A[n]: the array to reverse

Returns

  • 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.

Source Code:

Javascript

/*
 * Complete the 'reverseArray' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts INTEGER_ARRAY a as parameter.
 */

function reverseArray(a) {
    // Write your code here
   return a.reverse();
}

function main() {
    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const arrCount = parseInt(readLine().trim(), 10);

    const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));

    const res = reverseArray(arr);

    ws.write(res.join(' ') + '\n');

    ws.end();
}

Java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'reverseArray' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts INTEGER_ARRAY a as parameter.
     */

    public static List<Integer> reverseArray(List<Integer> list) {
    // Write your code here
    
    //List<Integer> reverse = new ArrayList<>(list.size());
    
    //1.using list iterator
    //List<Integer> reverse = new ArrayList<>(list.size());
    // ListIterator<Integer> itr = list.listIterator(list.size());
    
    // while(itr.hasPrevious())reverse.add(itr.previous());
    
    //2.for loop
    //List<Integer> reverse = new ArrayList<>(list.size());
    //  for (int i = list.size() - 1; i >= 0; i--) {
    //         reverse.add(list.get(i));
    //     }

    //3.using Collections.reverse()
    List<Integer> reverse = new ArrayList<>(list);
    Collections.reverse(reverse);
    
    return reverse;

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arrCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        List<Integer> res = Result.reverseArray(arr);

        bufferedWriter.write(
            res.stream()
                .map(Object::toString)
                .collect(joining(" "))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Leave a Reply