Queries for rotation and Kth character of the given string in constant time | C # implementation of the approach ; Function to perform the required queries on the given string ; Pointer pointing to the current starting character of the string ; For every query ; If the query is to rotate the string ; Update the pointer pointing to the starting character of the string ; Index of the kth character in the current rotation of the string ; Print the kth character ; Driver code
using System ; class GFG { static int size = 2 ; static void performQueries ( String str , int n , int [ , ] queries , int q ) { int ptr = 0 ; for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i , 0 ] == 1 ) { ptr = ( ptr + queries [ i , 1 ] ) % n ; } else { int k = queries [ i , 1 ] ; int index = ( ptr + k - 1 ) % n ; Console . WriteLine ( str [ index ] ) ; } } } public static void Main ( String [ ] args ) { String str = " abcdefgh " ; int n = str . Length ; int [ , ] queries = { { 1 , 2 } , { 2 , 2 } , { 1 , 4 } , { 2 , 7 } } ; int q = queries . GetLength ( 0 ) ; performQueries ( str , n , queries , q ) ; } }