Last active
August 19, 2016 02:31
-
-
Save dgodfrey206/a447f80e40936a1ea72d906d738de4a1 to your computer and use it in GitHub Desktop.
Formula to generate index of element of array after k insertions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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