ani_10's blog

By ani_10, history, 3 weeks ago, In English,
    1. PROBLEM LINK

https://www.hackerrank.com/challenges/circular-array-rotation/copy-from/168699996

//Complete the circularArrayRotation function below.

// Complete the circularArrayRotation function below.

vector circularArrayRotation(vector < int > a, int k, vector < int > queries) {

int n = a.size(),x;

int Q = queries.size();

vector < int > b(n,0);

vector < int > ans(n,0);

for(int i=0;i<n;i++){

b[(i+k)%n] = a[i];

}

for(int i=0;i<Q;i++)

{

    int m = queries[i];

    int ani = b[m];


    ans.push_back(ani);

}

return ans;

}

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

string nkq_temp;

getline(cin, nkq_temp);


vector<string> nkq = split_string(nkq_temp);

int n = stoi(nkq[0]);


int k = stoi(nkq[1]);


int q = stoi(nkq[2]);


string a_temp_temp;


getline(cin, a_temp_temp);

vector<string> a_temp = split_string(a_temp_temp);

vector<int> a(n);

for (int i = 0; i < n; i++)

{

int a_item = stoi(a_temp[i]);

    a[i] = a_item;

}

vector<int> queries(q);

for (int i = 0; i < q; i++)

{

int queries_item;

    cin >> queries_item;

    cin.ignore(numeric_limits<streamsize>::max(), '\n');


    queries[i] = queries_item;

}

vector<int> result = circularArrayRotation(a, k, queries);

for (int i = 0; i < result.size(); i++)

{

fout << result[i];


    if (i != result.size() - 1)

{

fout << "\n";

    }

}

fout << "\n";

fout.close();

return 0;

}

 
 
 
 
  • Vote: I like it
  • -16
  • Vote: I do not like it

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

First of all — you forgot to specify the type of some of your vectors (i.e. vector<int> circularArrayRotation(vector<int> a, int k, vector<int> queries) {).

Secondly — int n is not the same as int N — so your N variable is equal to 0.

Also, when you type vector<int> b it's empty so you can't use [] operator (try to change it to vector<int> b(N, 0);)

Next, if you use .push_back(), don't fill vector with 0's (vector<int> ans; instead of vector<int> ans(N,0);).

And lastly — you can't read queries from standard input, so:

for(int i=0;i<Q;i++){
    int m = queries[i];
    int ani = b[m];
    ans.push_back(ani);
}

I hope this will help you and you will manage to solve this problem :)

PS. Next time try to format your code

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).

»
3 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

may I know the reason why -16 in my blog I know its not informative ...