Insert a Character in a Rotated String
Last Updated :
22 Dec, 2022
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”
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++
#include <bits/stdc++.h>
using namespace std;
char Insert( char arr[], int n, int k)
{
int ind = 0;
int sz = 0;
string s = "" ;
char ch = arr[0];
s += ch;
sz = 1;
ind = 0;
for ( int i = 1; i < n; i++) {
ch = arr[i];
string s1 = s.substr(0, ind + 1);
int temp = k % sz;
int ro = temp - min(temp, sz - ind - 1);
if (ro == 0) {
string s2 = s.substr(ind + 1, temp);
string s3 = s.substr(ind + temp + 1,
sz - ind - temp - 1);
s = s1 + s2 + ch + s3;
ind = s1.size() + s2.size();
sz = s.size();
}
else {
string s2 = s.substr(0, ro);
string s3 = s.substr(ro, sz - ro);
s = s2 + ch + s3;
ind = s2.size();
sz = s.size();
}
}
if (ind == 0)
return s[sz - 1];
else
return s[ind - 1];
}
int main()
{
char arr[] = { '1' , '2' , '3' , '4' , '5' };
int k = 2;
int n = sizeof (arr) / sizeof (arr[0]);
cout << Insert(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static char Insert( char arr[], int n, int k)
{
int ind = 0 ;
int sz = 0 ;
String s = "" ;
char ch = arr[ 0 ];
s += ch;
sz = 1 ;
ind = 0 ;
for ( int i = 1 ; i < n; i++)
{
ch = arr[i];
String s1 = s.substring( 0 , ind + 1 );
int temp = k % sz;
int ro = temp - Math.min(temp, sz - ind - 1 );
if (ro == 0 )
{
String s2 = s.substring(ind + 1 ,
ind + 1 + temp);
String s3 = s.substring(ind + temp + 1 , sz);
s = s1 + s2 + ch + s3;
ind = s1.length() + s2.length();
sz = s.length();
}
else
{
String s2 = s.substring( 0 , ro);
String s3 = s.substring(ro, sz);
s = s2 + ch + s3;
ind = s2.length();
sz = s.length();
}
}
if (ind == 0 )
{
return s.charAt(sz - 1 );
}
else
{
return s.charAt(ind - 1 );
}
}
public static void main(String []args)
{
char arr[] = { '1' , '2' , '3' , '4' , '5' };
int k = 2 ;
int n = arr.length;
System.out.println(Insert(arr, n, k));
}
}
|
Python3
def insert(arr: list , n: int , k: int ) - > chr :
ind = 0
sz = 0
s = ""
ch = arr[ 0 ]
s + = ch
sz = 1
ind = 0
for i in range ( 1 , n):
ch = arr[i]
s1 = s[ 0 :ind + 1 ]
temp = k % sz
ro = temp - min (temp, sz - ind - 1 )
if ro = = 0 :
s2 = s[ind + 1 :ind + 1 + temp]
s3 = s[ind + temp + 1 :sz]
s = s1 + s2 + ch + s3
ind = len (s1) + len (s2)
sz = len (s)
else :
s2 = s[:ro]
s3 = s[ro:sz]
s = s2 + ch + s3
ind = len (s2)
sz = len (s)
if ind = = 0 :
return s[sz - 1 ]
else :
return s[ind - 1 ]
if __name__ = = "__main__" :
arr = [ '1' , '2' , '3' , '4' , '5' ]
k = 2
n = len (arr)
print (insert(arr, n, k))
|
C#
using System;
class GFG{
static char Insert( char [] arr, int n, int k)
{
int ind = 0;
int sz = 0;
String s = "" ;
char ch = arr[0];
s += ch;
sz = 1;
ind = 0;
for ( int i = 1; i < n; i++)
{
ch = arr[i];
string s1 = s.Substring(0, ind + 1);
int temp = k % sz;
int ro = temp - Math.Min(temp, sz - ind - 1);
if (ro == 0)
{
string s2 = s.Substring(ind + 1, temp);
string s3 = s.Substring(ind + temp + 1,
sz - ind - temp - 1);
s = s1 + s2 + ch + s3;
ind = s1.Length + s2.Length;
sz = s.Length;
}
else
{
string s2 = s.Substring(0, ro);
string s3 = s.Substring(ro, sz - ro);
s = s2 + ch + s3;
ind = s2.Length;
sz = s.Length;
}
}
if (ind == 0)
{
return s[sz - 1];
}
else
{
return s[ind - 1];
}
}
static public void Main()
{
char [] arr = { '1' , '2' , '3' , '4' , '5' };
int k = 2;
int n = arr.Length;
Console.WriteLine(Insert(arr, n, k));
}
}
|
Javascript
<script>
function Insert(arr,n,k)
{
let ind = 0;
let sz = 0;
let s = "" ;
let ch = arr[0];
s += ch;
sz = 1;
ind = 0;
for (let i = 1; i < n; i++)
{
ch = arr[i];
let s1 = s.substring(0, ind + 1);
let temp = k % sz;
let ro = temp - Math.min(temp, sz - ind - 1);
if (ro == 0)
{
let s2 = s.substring(ind + 1,
ind + 1 + temp);
let s3 = s.substring(ind + temp + 1, sz);
s = s1 + s2 + ch + s3;
ind = s1.length + s2.length;
sz = s.length;
}
else
{
let s2 = s.substring(0, ro);
let s3 = s.substring(ro, sz);
s = s2 + ch + s3;
ind = s2.length;
sz = s.length;
}
}
if (ind == 0)
{
return s[sz - 1];
}
else
{
return s[ind - 1];
}
}
let arr=['1 ', ' 2 ', ' 3 ', ' 4 ', ' 5' ];
let k = 2;
let n = arr.length;
document.write(Insert(arr, n, k));
</script>
|
Time Complexity: O(n2), where n is the length of the given string.
Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...