### ani_10's blog

By ani_10, history, 3 weeks ago, , 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);

int k = stoi(nkq);

int q = stoi(nkq);

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;

} Comments (8)
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » 3 weeks ago, # | ← Rev. 2 →   First of all — you forgot to specify the type of some of your vectors (i.e. vector circularArrayRotation(vector a, int k, vector queries) {).Secondly — int n is not the same as int N — so your N variable is equal to 0.Also, when you type vector b it's empty so you can't use [] operator (try to change it to vector b(N, 0);)Next, if you use .push_back(), don't fill vector with 0's (vector ans; instead of vector ans(N,0);).And lastly — you can't read queries from standard input, so: for(int i=0;i
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » Auto comment: topic has been updated by ani_10 (previous revision, new revision, compare).
 » may I know the reason why -16 in my blog I know its not informative ...