Complete reverseArray C++ Solution

I came through a question on LeetCode to complete a reverseArray function. We have an array and we have to return the array reversing all the elements inside it. For Example, we have an array “X” which has elements [1,2,3,4,5,6]. We have to write a function that will return the [6,5,4,3,2,1].

reverseArray

Question

Complete the reverseArray function. Parameters of reveseArray is a vector that we have to reverse. The return value must also be a vector. To read the Complete Question visit HERE

This question on LeetCode has Difficulty Level: Easy and Max Marks: 10

Note: In this problem a special type of array vectors are used, which are types of dynamic arrays. They have the ability to resize themselves while inserting and deleting elements.

Given Code

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'reverseArray' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts INTEGER_ARRAY a as parameter.
 */

vector<int> reverseArray(vector<int> a) {

}

My Solution

  • Firstly, I initialized a temporary vector (vector<int> temp) to store the reverse array in it.
vector<int> reverseArray(vector<int> a) {
   vector<int> temp;
}
  • Secondly, I initialized a loop starting from end of the given vector (a) to start of the given vector.


vector<int> reverseArray(vector<int> a) {
  
   vector<int> temp;
   for(int i = a.size() - 1; i != -1; i ++)
   {
   
   }                  
}

  • Finally, I stored the elements present in (a) starting from last into the temp array. Then returned the temp array.
vector<int> reverseArray(vector<int> a) {
  
   vector<int> temp;
   for(int i = a.size() - 1; i != -1; i ++)
   {
    temp.push_back(a[i]);   
   }  

    return temp;
}

Complete Code

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);
vector<string> split(const string &);

/*
 * Complete the 'reverseArray' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts INTEGER_ARRAY a as parameter.
 */

vector<int> reverseArray(vector<int> a) {
    vector<int> temp;
for(int i = a.size()-1; i != -1; i --){
    temp.push_back(a[i]);
}
return temp;
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string arr_count_temp;
    getline(cin, arr_count_temp);

    int arr_count = stoi(ltrim(rtrim(arr_count_temp)));

    string arr_temp_temp;
    getline(cin, arr_temp_temp);

    vector<string> arr_temp = split(rtrim(arr_temp_temp));

    vector<int> arr(arr_count);

    for (int i = 0; i < arr_count; i++) {
        int arr_item = stoi(arr_temp[i]);

        arr[i] = arr_item;
    }

    vector<int> res = reverseArray(arr);

    for (size_t i = 0; i < res.size(); i++) {
        fout << res[i];

        if (i != res.size() - 1) {
            fout << " ";
        }
    }

    fout << "\n";

    fout.close();

    return 0;
}

string ltrim(const string &str) {
    string s(str);

    s.erase(
        s.begin(),
        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
    );

    return s;
}

string rtrim(const string &str) {
    string s(str);

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
        s.end()
    );

    return s;
}

vector<string> split(const string &str) {
    vector<string> tokens;

    string::size_type start = 0;
    string::size_type end = 0;

    while ((end = str.find(" ", start)) != string::npos) {
        tokens.push_back(str.substr(start, end - start));

        start = end + 1;
    }

    tokens.push_back(str.substr(start));

    return tokens;
}

Output

Input : [1,2,1,5]
Output: [5,1,2,1]

input: [1,2,1,2,1,1,6,7,2,8,9]
Output: [9,8,2,7,6,1,1,2,1,2,1]

For related discussion on this problem visit HERE

READ MORE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Databases related posts Visit HERE

Python-related posts Visit HERE

C++ related posts Visit HERE

Data Science related posts visit HERE

Share the Knowledge