Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Copy And Reverse The Array

Easy
0/40
Average time to solve is 10m
profile
Contributed by
64 upvotes
Asked in companies
CognizantAmdocsEncore Capital Group

Problem statement

You are given an array 'ARR' consisting of 'N' non-negative integers, your task is to copy the elements of 'ARR' into another array 'COPY_ARR' in reverse order.

For example:

If 'ARR' contains the following elements [ 1, 2, 3, 4, 5 ], then you should return another array that is equal to [ 5, 4, 3, 2, 1].
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of the input contains a single integer T, denoting the number of test cases.

The first line of each test case contains a single integer N, denoting the length of the array 'ARR'.

The second line of each test case contains N space-separated integers, denoting the elements of the array.
Output Format :
For each test case print, N space-separated integers, where the ith integer denotes the ith element of the array 'COPY_ARR'.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10^2
1 <= N <= 10^4
0 <= ARR[i] <= 10^9

Time Limit: 1 sec
Sample Input 1 :
2
5
1 2 3 4 5
4
5 8 2 1
Sample Output 1 :
5 4 3 2 1
1 2 8 5
Explanation For Sample Input 1 :
The reverse of [1, 2, 3, 4, 5] is [5, 4, 3, 2,1]
The reverse of [5, 8, 2, 1] is [1, 2, 8, 5].
Sample Input 2 :
3
3
3 2 1
4
8  6 2 4
5
1 3 5 4 2
Sample Output 2 :
1 2 3
4 2 6 8
2 4 5 3 1
Explanation For Sample Input 2 :
The reverse of [3, 2, 1] is [1, 2, 3].
The reverse of [8, 6, 2, 4] is [4, 2, 6, 8].
The reverse of [1, 3, 5, 4, 2] is [2, 4, 5, 3, 1].
Hint

Try to go from end to beginning in the ARR.

Approaches (1)
Array Traversal
  1. Declare a new array COPY_ARR of length equal to that of ARR, let the length be N.
  2. Iterate the array ARR and copy the value at ith position in ARR to N-i-1 th position in COPY_ARR. That is COPY_ARR[N-i-1] = ARR[i]
Time Complexity

O(N) , where N is the length of the array ARR.

 

The time complexity is O(N) because we are traversing the complete array of N elements

Space Complexity

O(1).

 

 As we are using constant extra memory.

Code Solution
(100% EXP penalty)
Copy And Reverse The Array
All tags
Sort by
Search icon

Interview problems

Reverse the array. Python

def copyAndReverse(arr):

    # Write your code here

    a=arr[::-1]

    return a

6 views
0 replies
0 upvotes

Interview problems

simplest solution using while loop (C++)

#include <bits/stdc++.h> 
vector<int> copyAndReverse(vector<int> arr, int n) {
	// Write your code here.
	
	vector<int> ans;
	int s =0,e=n-1;
	while(s<=e){
		ans.push_back(arr[e]);
		e--;
	}
	return ans;
	
}	
63 views
0 replies
0 upvotes

Interview problems

I solution this question with C++ and Python, here is the solution 😎

  • C++ solution
#include <bits/stdc++.h> 

vector<int> copyAndReverse(vector<int> arr, int n) {

	// Write your code here.

	vector<int>reverseArr;

	for(int i=0; i<n; i++){

       reverseArr.push_back(arr[n-1-i]);

	}

	return reverseArr;

}

 

  • Python solution

 

from os import *
from sys import *
from collections import *
from math import *

def copyAndReverse(arr):
    # Write your code here
    arr.reverse()
    return arr

 

 

37 views
0 replies
0 upvotes

Interview problems

A one line c++ code!!!

#include <bits/stdc++.h> 

vector<int> copyAndReverse(vector<int> arr, int n) {

    reverse(arr.begin(),arr.end());

    return arr;

}   

programming

35 views
0 replies
0 upvotes

Interview problems

C++ Easy Solution

#include <bits/stdc++.h> 

vector<int> copyAndReverse(vector<int> arr, int n) {

    // Write your code here.

    vector<int>ans(arr);

    reverse(ans.begin(),ans.end());

    return ans;

}   

41 views
0 replies
0 upvotes

Interview problems

Perfect Solution

#include <bits/stdc++.h> 

vector<int> copyAndReverse(vector<int> arr, int n) {

    // Write your code here.

    vector<int> nums;

    

    for(int i=n-1;i>=0;i--)

    {

        nums.push_back(arr[i]);

    }

    return nums;

}   

25 views
0 replies
0 upvotes

Interview problems

Easy Solution || Java

Easy Solution || Java

 

import java.util.* ;

import java.io.*; 

public class Solution {

    public static int[] copyAndReverse(int[] arr, int n) {

        // Write your code here.

        int p=n-1;

        for(int i=0;i<n/2;i++){

            int temp=arr[i];

            arr[i]=arr[p];

            arr[p]=temp;

            p--;

        }

        return arr;

    }

}

 

60 views
0 replies
0 upvotes

Interview problems

C++ solution without using reverse function

vector<int>v;

 

    for(int i=n-1; i>=0; i--){

        v.push_back(arr[i]);

    }

 

    return v;

38 views
0 replies
0 upvotes

Interview problems

ONELINER CODE 🔥

#include <bits/stdc++.h>

vector<int> copyAndReverse(vector<int> arr, int n) {

// Write your code here.

reverse(arr.begin(),arr.end());

return arr;

}

26 views
0 replies
0 upvotes

Interview problems

3 line code

#include <bits/stdc++.h> 

vector<int> copyAndReverse(vector<int> arr, int n) {

    // Write your code here.

    vector<int> copy;

    for(int i = n-1; i >=0; i--) copy.push_back(arr[i]);

    return copy;

}   

13 views
0 replies
1 upvote
Full screen
Console