Day 7 : Arrays
Problem
Welcome to Day 7! Check out a video review of arrays here, or just jump right into the problem.
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.
You'll be given an array of N integers, and you have to print the integers in reverse order.
Good luck!
Input Format
The first line of input contains N, the number of integers. The next line contains N integers separated by a space.
Constraints
1≤N≤1000
1≤Ai≤10000, where Ai is the ith integer in the array.
Output Format
Print the N integers of the array in the reverse order on a single line separated by a space.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.
Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.
HAVE A GOOD DAY 😁
Solution:
( java )
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// Get the input
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int[] array = new int[length];
for (int i = 0; i < length; i++) {
array[i] = sc.nextInt();
}
// Var holding our new string
String result = "";
for (int i = array.length - 1; i >= 0; i--) {
result = result + array[i] + " ";
}
System.out.println(result);
}
}
Solution :
( c++ )
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> arr(n);
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> arr[arr_i];
}
for(int arr_i = n-1;arr_i >= 0;arr_i--){
cout << arr[arr_i] << " ";
}
cout << endl;
return 0;
}