# Insert a Character in a Rotated String

Given an array of characters arr[] of size N and an integer K. You have to insert the characters into an empty string one by one such that every insertion is done after K positions to the right from the previous insertion and the string is circular. The task is to find the character after which the last insertion is done

Examples:

Input: arr[] = {‘1’, ‘2’, ‘3’, ‘4’, ‘5’}, K = 2
Output: 1
After the first insertion string becomes “1”
After the second insertion string becomes “12”
After third insertion, as the previous insertion was at position 2.
So, the current insertion has to be at 2 + 2 i.e. 4th position.
Since, the string is circular so moving 2 positions to the right will
be “1|2” -> “12|” and the final string will be “123”
After the fourth insertion, “123” -> “1|23” -> “12|3” i.e. “1243”
After the fifth insertion, “1243” -> “1243|” -> “1|243” i.e. “15243”
The last character inserted after the character ‘1’

Input: arr[] = {‘1’, ‘2’, ‘3’, ‘4’, ‘5’}, K = 1
Output: 1
Final string is “15324”

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For every character, insert it in the required position and also keep track of the previous position and the character after which the previous insertion was done. When all the characters have been inserted, print the character after which the last insertion was done.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to insert the character ` `char` `Insert(``char` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``// To store last position where ` `    ``// the insertion is done ` `    ``int` `ind = 0; ` ` `  `    ``// To store size of the string ` `    ``int` `sz = 0; ` ` `  `    ``// To store the modified string ` `    ``string s = ``""``; ` ` `  `    ``// To store characters ` `    ``char` `ch = arr; ` ` `  `    ``// Add first character to the string ` `    ``s += ch; ` ` `  `    ``// Update the size ` `    ``sz = 1; ` ` `  `    ``// Update the index of last insertion ` `    ``ind = 0; ` ` `  `    ``// Insert all other characters to the string ` `    ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `        ``// Take the character ` `        ``ch = arr[i]; ` ` `  `        ``// Take substring upto ind ` `        ``string s1 = s.substr(0, ind + 1); ` ` `  `        ``// Take modulo value of k with ` `        ``// the size of the string ` `        ``int` `temp = k % sz; ` ` `  `        ``// Check if we need to move to ` `        ``// the start of the string ` `        ``int` `ro = temp - min(temp, sz - ind - 1); ` ` `  `        ``// If we don't need to move to start of the string ` `        ``if` `(ro == 0) { ` ` `  `            ``// Take substring from upto temp ` `            ``string s2 = s.substr(ind + 1, temp); ` ` `  `            ``// Take substring which will be after ` `            ``// the inserted character ` `            ``string s3 = s.substr(ind + temp + 1, ` `                                 ``sz - ind - temp - 1); ` ` `  `            ``// Insert into the string ` `            ``s = s1 + s2 + ch + s3; ` ` `  `            ``// Store new inserted position ` `            ``ind = s1.size() + s2.size(); ` ` `  `            ``// Store size of the new string ` `            ``// Technically sz + 1 ` `            ``sz = s.size(); ` `        ``} ` ` `  `        ``// If we need to move to start of the string ` `        ``else` `{ ` ` `  `            ``// Take substring which will before ` `            ``// the inserted character ` `            ``string s2 = s.substr(0, ro); ` ` `  `            ``// Take substring which will be after ` `            ``// the inserted character ` `            ``string s3 = s.substr(ro, sz - ro); ` ` `  `            ``// Insert into the string ` `            ``s = s2 + ch + s3; ` ` `  `            ``// Store new inserted position ` `            ``ind = s2.size(); ` ` `  `            ``// Store size of the new string ` `            ``// Technically sz + 1 ` `            ``sz = s.size(); ` `        ``} ` `    ``} ` ` `  `    ``// Return the required character ` `    ``if` `(ind == 0) ` `        ``return` `s[sz - 1]; ` `    ``else` `        ``return` `s[ind - 1]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``char` `arr[] = { ``'1'``, ``'2'``, ``'3'``, ``'4'``, ``'5'` `}; ` ` `  `    ``int` `k = 2; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Function call ` `    ``cout << Insert(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to insert the character ` `def` `insert(arr: ``list``, n: ``int``, k: ``int``) ``-``> ``chr``: ` ` `  `    ``# To store last position where ` `    ``# the insertion is done ` `    ``ind ``=` `0` ` `  `    ``# To store size of the string ` `    ``sz ``=` `0` ` `  `    ``# To store the modified string ` `    ``s ``=` `"" ` ` `  `    ``# To store characters ` `    ``ch ``=` `arr[``0``] ` ` `  `    ``# Add first character to the string ` `    ``s ``+``=` `ch ` ` `  `    ``# Update the size ` `    ``sz ``=` `1` ` `  `    ``# Update the index of last insertion ` `    ``ind ``=` `0` ` `  `    ``# Insert all other characters to the string ` `    ``for` `i ``in` `range``(``1``, n): ` ` `  `        ``# Take the character ` `        ``ch ``=` `arr[i] ` ` `  `        ``# Take substring upto ind ` `        ``s1 ``=` `s[``0``:ind ``+` `1``] ` ` `  `        ``# Take modulo value of k with ` `        ``# the size of the string ` `        ``temp ``=` `k ``%` `sz ` ` `  `        ``# Check if we need to move to ` `        ``# the start of the string ` `        ``ro ``=` `temp ``-` `min``(temp, sz ``-` `ind ``-` `1``) ` ` `  `        ``# If we don't need to move to start of the string ` `        ``if` `ro ``=``=` `0``: ` ` `  `            ``# Take substring from upto temp ` `            ``s2 ``=` `s[ind ``+` `1``:ind ``+` `1` `+` `temp] ` ` `  `            ``# Take substring which will be after ` `            ``# the inserted character ` `            ``s3 ``=` `s[ind ``+` `temp ``+` `1``:sz] ` ` `  `            ``# Insert into the string ` `            ``s ``=` `s1 ``+` `s2 ``+` `ch ``+` `s3 ` ` `  `            ``# Store new inserted position ` `            ``ind ``=` `len``(s1) ``+` `len``(s2) ` ` `  `            ``# Store size of the new string ` `            ``# Technically sz + 1 ` `            ``sz ``=` `len``(s) ` ` `  `        ``# If we need to move to start of the string ` `        ``else``: ` ` `  `            ``# Take substring which will before ` `            ``# the inserted character ` `            ``s2 ``=` `s[:ro] ` ` `  `            ``# Take substring which will be after ` `            ``# the inserted character ` `            ``s3 ``=` `s[ro:sz] ` ` `  `            ``# Insert into the string ` `            ``s ``=` `s2 ``+` `ch ``+` `s3 ` ` `  `            ``# Store new inserted position ` `            ``ind ``=` `len``(s2) ` ` `  `            ``# Store size of the new string ` `            ``# Technically sz + 1 ` `            ``sz ``=` `len``(s) ` ` `  `    ``# Return the required character ` `    ``if` `ind ``=``=` `0``: ` `        ``return` `s[sz ``-` `1``] ` `    ``else``: ` `        ``return` `s[ind ``-` `1``] ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``arr ``=` `[``'1'``, ``'2'``, ``'3'``, ``'4'``, ``'5'``] ` `    ``k ``=` `2` `    ``n ``=` `len``(arr) ` ` `  `    ``# Function call ` `    ``print``(insert(arr, n, k)) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

Output:

```1
```

My Personal Notes arrow_drop_up pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ManasChhabra2, sanjeev2552

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.