-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathk.cpp
More file actions
54 lines (41 loc) · 1.06 KB
/
k.cpp
File metadata and controls
54 lines (41 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define size 2
// Function to perform the required
// queries on the given string
void performQueries(string str, int n,
int queries[][size], int q)
{
// Pointer pointing to the current starting
// character of the string
int ptr = 0;
// For every query
for (int i = 0; i < q; i++) {
// If the query is to rotate the string
if (queries[i][0] == 1) {
// Update the pointer pointing to the
// starting character of the string
ptr = (ptr + queries[i][1]) % n;
}
else {
int k = queries[i][1];
// Index of the kth character in the
// current rotation of the string
int index = (ptr + k - 1) % n;
// Print the kth character
cout << str[index] << "\n";
}
}
}
// Driver code
int main()
{
string str = "abcdefgh";
int n = str.length();
int queries[][size] = { { 1, 2 }, { 2, 2 },
{ 1, 4 }, { 2, 7 } };
int q = sizeof(queries) / sizeof(queries[0]);
performQueries(str, n, queries, q);
return 0;
}