what is array
Array is a collection of same type placed in contiguous memory location that can be individually referenced by using an index of unique element.
We covered in the article the following points below
- problem analysis
- input format
- constraints
- output format
- sample input
- sample output
- source code
- source code explanation
- video explanation
- feedback
Array Reversal Solutions |
Problem analysis
you have to give input in the first line the number of arrays in the next line you have to show the arrays in the reverse order.
Input Format
The first line of the input contains N, where N is the number of integers The next line contains N space-separated integers.
Constraints
1<=N<+1000
1<=A[i]<=10000, where A[i] is the ith integer in the array.
Output Format
Print the N integers of the array in the reverse order, space-separated on a single line.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
Source Code
#include<iostream>
using namespace std;
int main()
{
int n;
int arr[1000];
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<n/2;i++)
{
int temp;
temp=arr[i];
arr[i]=arr[n-1-i];
arr[n-1-i]=temp;
}
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
- First of all we added the required header files for the program .
- First we asked the user to enter the no of vectors you want to enter for reverse it, Then we declare an array of size 1000 because here our constraint is given up to 1000 so that is the reason , Then we run a for loop for entering the elements into array.
- THE BELOW LINE IS IMPORTANT READ CAREFULLY
- Here we run the loop n/2 times because our logic is to swap the first value with the last value so we continue this process till the 1/2 of the array so we run the loop n/2 times .
- Inside the for loop we declare a temp variable for swapping the elements and for checking the elements from last we use arr[n-1-i].
- Then again we use same for loop for displaying the elemets .
Also check Out
Also check Out Projects for Resume
Problems On C++
Q-2) Vectors In c++
Problems on TCS paper
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187
0 , 0 , 2 , 1 , 4 , 2 , 6 , 3 , 8 , 4 , 10 , 5 , 12 , 6 , 14 , 7 , 16 , 8
Feedback
- That is for Array Reversal C++ Hackerrank solution hope you enjoy the article and understand this explanation clearly ,
- if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.
THANKYOU!!
- That is for Array Reversal C++ Hackerrank solution hope you enjoy the article and understand this explanation clearly ,
- if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.
Post a Comment
Post a Comment
Please do not spam in comment