Reversing an array in C++ 14, my second array requires an extra element Hackerrank problem to reverse an array of size n.
https://www.hackerrank.com/challenges/30-arrays/
Sample Input:
4
1 4 3 2

Sample output:
2 3 4 1

Much of this code was already given, you can skip to the &quot;This is what I did&quot; section.
#include &lt;bits/stdc++.h&gt;

using namespace std;

vector&lt;string&gt; split_string(string);



int main()
{
    int n;
    cin &gt;&gt; n;
    cin.ignore(numeric_limits&lt;streamsize&gt;::max(), '\n');

    string arr_temp_temp;
    getline(cin, arr_temp_temp);

    vector&lt;string&gt; arr_temp = split_string(arr_temp_temp);

    vector&lt;int&gt; arr(n);
    vector&lt;int&gt; arr2(n);

    for (int i = 0; i &lt; n; i++) {
        int arr_item = stoi(arr_temp[i]);
         
        arr[i] = arr_item;
    }
    
    for (int i = 0; i &lt;= n; i++){
        arr2[n-i] = arr[i];       
    }
    for (int i = 1; i &lt;= n; i++){
        cout &lt;&lt; arr2[i] &lt;&lt; &quot; &quot;;       
    }
    return 0;
}

vector&lt;string&gt; split_string(string input_string) {
    string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &amp;x, const char &amp;y) {
        return x == y and x == ' ';
    });

    input_string.erase(new_end, input_string.end());

    while (input_string[input_string.length() - 1] == ' ') {
        input_string.pop_back();
    }

    vector&lt;string&gt; splits;
    char delimiter = ' ';

    size_t i = 0;
    size_t pos = input_string.find(delimiter);

    while (pos != string::npos) {
        splits.push_back(input_string.substr(i, pos - i));

        i = pos + 1;
        pos = input_string.find(delimiter, i);
    }

    splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));

    return splits;
}

This is what I did
I declared another array named array2 of size n to hold the reversed value which would then be printed.
I then wrote this snippet to reverse the array and copy it to array2:
  for (int i = 0; i &lt;= n; i++){
        arr2[n-i] = arr[i];       
    }
    for (int i = 1; i &lt;= n; i++){
        cout &lt;&lt; arr2[i] &lt;&lt; &quot; &quot;;       
    }

This code works, but I feel like I have made a mistake since I have iterated n+1 times in the for loop. I tried iterating n times but one element of the array got the value 0. Indexing is also done differently since I am indexing from 1 to n. This is confusing to me, because I declared array2 as size n. How and when did the vector get resized?
Is there a more elegant solution using n iterations? If not, why not?
The first couple of lines show that this is shoddy code:

#include &lt;bits/stdc++.h&gt;

using namespace std;


&lt;bits/stdc++.h&gt; is not a standard header, and dragging all of std into the default namespace is a maintainability disaster (see other Code Review answers, ad nauseum).
None of the code which reads from std::cin ever checks whether the reading was successful, and there's no error handling whatsoever.
arr is a terrible name for a vector.  arr2 is worse.
There's no need for handcrafted loops, when we could use std::copy_n() to read elements, and std::reverse() to reorder them.
Actually, there's no need to reorder the stored data - just write the output using a reverse iterator.

The low quality shows up when we analyse the memory accesses:
valgrind ./255602 &lt;&lt;&lt;$'4\n1 4 3 2 5'
==5474== Memcheck, a memory error detector
==5474== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5474== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==5474== Command: ./255602-orig
==5474== 
==5474== Invalid write of size 4
==5474==    at 0x10B466: main (255602.cpp:30)
==5474==  Address 0x4d49000 is 0 bytes after a block of size 16 alloc'd
==5474==    at 0x480BDEF: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5474==    by 0x10E741: __gnu_cxx::new_allocator&lt;int&gt;::allocate(unsigned long, void const*) (new_allocator.h:115)
==5474==    by 0x10E437: allocate (allocator.h:173)
==5474==    by 0x10E437: std::allocator_traits&lt;std::allocator&lt;int&gt; &gt;::allocate(std::allocator&lt;int&gt;&amp;, unsigned long) (alloc_traits.h:460)
==5474==    by 0x10DE3F: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_allocate(unsigned long) (stl_vector.h:346)
==5474==    by 0x10D60C: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_create_storage(unsigned long) (stl_vector.h:361)
==5474==    by 0x10CEA0: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_Vector_base(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:305)
==5474==    by 0x10C46C: std::vector&lt;int, std::allocator&lt;int&gt; &gt;::vector(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:511)
==5474==    by 0x10B3B6: main (255602.cpp:21)
==5474== 
==5474== Invalid read of size 4
==5474==    at 0x10B449: main (255602.cpp:30)
==5474==  Address 0x4d48fb0 is 0 bytes after a block of size 16 alloc'd
==5474==    at 0x480BDEF: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5474==    by 0x10E741: __gnu_cxx::new_allocator&lt;int&gt;::allocate(unsigned long, void const*) (new_allocator.h:115)
==5474==    by 0x10E437: allocate (allocator.h:173)
==5474==    by 0x10E437: std::allocator_traits&lt;std::allocator&lt;int&gt; &gt;::allocate(std::allocator&lt;int&gt;&amp;, unsigned long) (alloc_traits.h:460)
==5474==    by 0x10DE3F: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_allocate(unsigned long) (stl_vector.h:346)
==5474==    by 0x10D60C: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_create_storage(unsigned long) (stl_vector.h:361)
==5474==    by 0x10CEA0: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_Vector_base(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:305)
==5474==    by 0x10C46C: std::vector&lt;int, std::allocator&lt;int&gt; &gt;::vector(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:511)
==5474==    by 0x10B382: main (255602.cpp:20)
==5474== 
==5474== Invalid read of size 4
==5474==    at 0x10B495: main (255602.cpp:33)
==5474==  Address 0x4d49000 is 0 bytes after a block of size 16 alloc'd
==5474==    at 0x480BDEF: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==5474==    by 0x10E741: __gnu_cxx::new_allocator&lt;int&gt;::allocate(unsigned long, void const*) (new_allocator.h:115)
==5474==    by 0x10E437: allocate (allocator.h:173)
==5474==    by 0x10E437: std::allocator_traits&lt;std::allocator&lt;int&gt; &gt;::allocate(std::allocator&lt;int&gt;&amp;, unsigned long) (alloc_traits.h:460)
==5474==    by 0x10DE3F: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_allocate(unsigned long) (stl_vector.h:346)
==5474==    by 0x10D60C: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_M_create_storage(unsigned long) (stl_vector.h:361)
==5474==    by 0x10CEA0: std::_Vector_base&lt;int, std::allocator&lt;int&gt; &gt;::_Vector_base(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:305)
==5474==    by 0x10C46C: std::vector&lt;int, std::allocator&lt;int&gt; &gt;::vector(unsigned long, std::allocator&lt;int&gt; const&amp;) (stl_vector.h:511)
==5474==    by 0x10B3B6: main (255602.cpp:21)
==5474== 
2 3 4 1 ==5474== 
==5474== HEAP SUMMARY:
==5474==     in use at exit: 0 bytes in 0 blocks
==5474==   total heap usage: 9 allocs, 9 frees, 78,336 bytes allocated
==5474== 
==5474== All heap blocks were freed -- no leaks are possible
==5474== 
==5474== For counts of detected and suppressed errors, rerun with: -v
==5474== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)

Such failures fall short of working, in my opinion.

This is what a decent C++ program to do this looks like:
#include &lt;algorithm&gt;
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

int main()
{
    // read the inputs
    std::size_t n;
    std::cin &gt;&gt; n;
    if (!std::cin) {
        std::cerr &lt;&lt; &quot;Failed to read number of elements\n&quot;;
        return EXIT_FAILURE;
    }

    std::vector&lt;std::string&gt; elements;
    elements.reserve(n);
    std::copy_n(std::istream_iterator&lt;std::string&gt;{std::cin},
                n, std::back_inserter(elements));
    if (!std::cin) {
        std::cerr &lt;&lt; &quot;Failed to read element content\n&quot;;
        return EXIT_FAILURE;
    }

    // write the values, in reverse order
    std::copy(elements.crbegin(), elements.crend(),
              std::ostream_iterator&lt;std::string&gt;{std::cout, &quot; &quot;});
}

See - no loops or arithmetic required.
Or, if you prefer an exception-based error handling strategy:
#include &lt;algorithm&gt;
#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

// read size from stream, then read that many elements into a vector
template&lt;typename T&gt;
std::vector&lt;T&gt; read_size_and_vector(std::istream&amp; is)
{
    std::size_t n;
    is &gt;&gt; n;
    std::vector&lt;T&gt; elements;
    elements.reserve(n);
    std::copy_n(std::istream_iterator&lt;T&gt;{is}, n, std::back_inserter(elements));
    return elements;
}

int main()
{
    std::cin.exceptions(std::istream::badbit|std::istream::failbit);
    try {
        auto const elements = read_size_and_vector&lt;std::string&gt;(std::cin);
        std::copy(elements.rbegin(), elements.rend(),
                  std::ostream_iterator&lt;std::string&gt;{std::cout, &quot; &quot;});
        return EXIT_SUCCESS;
    } catch (std::ios_base::failure&amp;) {
        std::cerr &lt;&lt; &quot;Failed to read the input\n&quot;;
        return EXIT_FAILURE;
    }
}