Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active August 19, 2016 02:31
Show Gist options
  • Save dgodfrey206/a447f80e40936a1ea72d906d738de4a1 to your computer and use it in GitHub Desktop.
Save dgodfrey206/a447f80e40936a1ea72d906d738de4a1 to your computer and use it in GitHub Desktop.
Formula to generate index of element of array after k insertions
#include <iostream>
using namespace std;
// returns the element at index m in arr after k rotations
int at(int* arr, int n, int k, int m) {
return arr[((m + (n - (k % n))) % n)];
}
int main() {
int n, k, q, i, j, m;
int* arr;
cin >> n >> k >> q;
arr = new int[n];
for (i = 0; i < n; ++i)
cin >> arr[i];
for (j = 0; j < q; ++j) {
cin >> m;
cout << at(arr, n, k, m) << '\n';
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment