Sequence Rotation Problem in Hackerrank

So, its 11:44 am, 4th of June and I was trying to implement the 3 problems which I had written earlier, this was the first one which had a logical error and then I had to see the example statement and again modify my algorithm, which went well on the first attempt only, and I think that this was quite interesting problem so I thought why not put this in a blog post for evading the effacing of the memory. Well I have two more left for the day, and then I'll probably hit the hay.

I didn't get time for duo today too, I am thinking that tomorrow I'll probably invest about two or three hours in duo, to get to 50,000. If you know it then you know it...Peace Out! 

#include <bits/stdc++.h>


using namespace std;

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

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

vector<int> permutationEquation(vector<int> p) {
vector<int> ans;
int n=p.size();
int num;
for(int i=1;i<=n;i++){
  //Traversing through the p array considering the i'th element everytime so we can find if we have any value which is equal to i, that is numbers from 1 to n, in the vector array p, and if we do, then again search for the number which is plus one of i and in whichever index it is just push its next number to the ans vector array.
        
    for(int j=0;j<n;j++){
        if(p[j]==i){//If we find the number i in p array then we have to search for its +1 in the array, which we have considered num in this case below.
            num=j+1;
            for(int k=0;k<n;k++){
                if(p[k]==num){//Now if we get that number we just have to push it to the ans array.This was required by the problem to this in order which required this second loop.
                    ans.push_back(k+1);
                }
            }
        }
    }
}
return ans;
}

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

    string n_temp;
    getline(cin, n_temp);

    int n = stoi(ltrim(rtrim(n_temp)));

    string p_temp_temp;
    getline(cin, p_temp_temp);

    vector<string> p_temp = split(rtrim(p_temp_temp));

    vector<int> p(n);

    for (int i = 0; i < n; i++) {
        int p_item = stoi(p_temp[i]);

        p[i] = p_item;
    }

    vector<int> result = permutationEquation(p);

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

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

    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<intint>(isspace)))
    );

    return s;
}

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

    s.erase(
        find_if(s.rbegin(), s.rend(), not1(ptr_fun<intint>(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;
}


Comments

Popular Posts