Tuesday, July 12, 2016

Hacker Rank 30 days of code Day 7: Arrays

The problem:
Objective
Today, we're learning about the Array data structure. Check out the Tutorial tab for learning materials and an instructional video!
Task
Given an array, , of  integers, print 's elements in reverse order as a single line of space-separated numbers.
Input Format
The first line contains an integer,  (the size of our array).
The second line contains  space-separated integers describing array 's elements.
Constraints
  • , where  is the  integer in the array.
Output Format
Print the elements of array  in reverse order as a single line of space-separated numbers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1

My submission:

 #include <map>  
 #include <set>  
 #include <list>  
 #include <cmath>  
 #include <ctime>  
 #include <deque>  
 #include <queue>  
 #include <stack>  
 #include <string>  
 #include <bitset>  
 #include <cstdio>  
 #include <limits>  
 #include <vector>  
 #include <climits>  
 #include <cstring>  
 #include <cstdlib>  
 #include <fstream>  
 #include <numeric>  
 #include <sstream>  
 #include <iostream>  
 #include <algorithm>  
 #include <unordered_map>  
 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];  
   }  
   int halfLength = n / 2;  
   for (int i=0; i<halfLength; i++)  
   {  
     int temp = arr[i];  
     arr[i] = arr[n-1-i];  
     arr[n-1-i] = temp;  
   }  
   for(int j = 0; j<n;j++){  
     printf("%d ",arr[j]);  
   }  
   return 0;  
 }  

No comments:

Post a Comment