codeparrot/xlcost-text-to-code huggingface dataset:
Count of substrings of length K with exactly K distinct characters | Java program to find the count of k length substrings with k distinct characters using sliding window ; Function to return the required count of substrings ; Store the count ; Store the count of distinct characters in every window ; Store the frequency of the first K length substring ; Increase frequency of i - th character ; If K distinct characters exist ; Traverse the rest of the substring ; Increase the frequency of the last character of the current substring ; Decrease the frequency of the first character of the previous substring ; If the character is not present in the current substring ; If the count of distinct characters is 0 ; Return the count ; Driver code ; string str ; integer K ; Print the count of K length substrings with k distinct characters
import java . util . * ; class GFG { public static int countSubstrings ( String str , int K ) { int N = str . length ( ) ; int answer = 0 ; Map < Character , Integer > map = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( map . get ( str . charAt ( i ) ) == null ) { map . put ( str . charAt ( i ) , 1 ) ; } else { map . put ( str . charAt ( i ) , map . get ( str . charAt ( i ) ) + 1 ) ; } } if ( map . size ( ) == K ) answer ++ ; for ( int i = K ; i < N ; i ++ ) { if ( map . get ( str . charAt ( i ) ) == null ) { map . put ( str . charAt ( i ) , 1 ) ; } else { map . put ( str . charAt ( i ) , map . get ( str . charAt ( i ) ) + 1 ) ; } map . put ( str . charAt ( i - K ) , map . get ( str . charAt ( i - K ) ) - 1 ) ; if ( map . get ( str . charAt ( i - K ) ) == 0 ) { map . remove ( str . charAt ( i - K ) ) ; } if ( map . size ( ) == K ) { answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { String str = " aabcdabbcdc " ; int K = 3 ; System . out . println ( countSubstrings ( str , K ) ) ; } }
Check if a string is a scrambled form of another string | Java Program to check if a given string is a scrambled form of another string ; map declaration for storing key value pair means for storing subproblem result ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; make key of type string for search in map ; checking if both string are before calculated or not if calculated means find in map then return it 's  value ; declaring flag variable to store result ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; add key & flag value to map ( store for future use ) so next time no required to calculate it again ; If none of the above conditions are satisfied ; Driver Code
import java . util . * ; public class Main { static HashMap < String , Boolean > mp = new HashMap < String , Boolean > ( ) ; static boolean isScramble ( String S1 , String S2 ) { if ( S1 . length ( ) != S2 . length ( ) ) { return false ; } int n = S1 . length ( ) ; if ( n != 0 ) { return true ; } if ( S1 == S2 ) { return true ; } String copy_S1 = S1 , copy_S2 = S2 ; char [ ] t1 = copy_S1 . toCharArray ( ) ; char [ ] t2 = copy_S2 . toCharArray ( ) ; Arrays . sort ( t1 ) ; Arrays . sort ( t2 ) ; copy_S1 = new String ( t1 ) ; copy_S2 = new String ( t2 ) ; if ( ! copy_S1 . equals ( copy_S2 ) ) { return false ; } String key = ( S1 + " ▁ " + S2 ) ; if ( mp . containsKey ( key ) ) { return mp . get ( key ) ; } boolean flag = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( i , n ) ) ) { flag = true ; return true ; } if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( n - i , n ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( 0 , n - i ) ) ) { flag = true ; return true ; } } mp . put ( key , flag ) ; return false ; } public static void main ( String [ ] args ) { String S1 = " coder " ; String S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Print the longest palindromic prefix of a given string | Java program for the above approach ; Function to find the longest prefix which is palindromic ; Find the length of the given String ; For storing the length of longest prefix palindrome ; Loop to check the subString of all length from 1 to N which is palindrome ; String of length i ; To store the reversed of temp ; Reversing String temp2 ; If String temp is palindromic then update the length ; Print the palindromic String of max_len ; Driver Code ; Given String ; Function Call
import java . util . * ; class GFG { static void LongestPalindromicPrefix ( String s ) { int n = s . length ( ) ; int max_len = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { String temp = s . substring ( 0 , len ) ; String temp2 = temp ; temp2 = reverse ( temp2 ) ; if ( temp . equals ( temp2 ) ) { max_len = len ; } } System . out . print ( s . substring ( 0 , max_len ) ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " abaab " ; LongestPalindromicPrefix ( str ) ; } }
K | Java program to find the K - th lexicographical String of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of Strings that can be represented by a String of length N ; Store the Kth lexicographical String ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
class GFG { static String find_kth_String_of_n ( int n , int k ) { int [ ] d = new int [ n ] ; for ( int i = n - 1 ; i > - 1 ; i -- ) { d [ i ] = k % 26 ; k /= 26 ; } if ( k > 0 ) return " - 1" ; String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( char ) ( d [ i ] + ( ' a ' ) ) ; return s ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 10 ; k -= 1 ; System . out . println ( find_kth_String_of_n ( n , k ) ) ; } }
Length of the smallest substring which contains all vowels | Java program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
import java . util . * ; class GFG { public static int findMinLength ( String s ) { int n = s . length ( ) ; HashMap < Character , Integer > counts = new HashMap < > ( ) ; Vector < Integer > indices = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' || s . charAt ( i ) == ' e ' || s . charAt ( i ) == ' o ' || s . charAt ( i ) == ' i ' || s . charAt ( i ) == ' u ' ) { if ( counts . containsKey ( s . charAt ( i ) ) ) { counts . replace ( s . charAt ( i ) , counts . get ( s . charAt ( i ) ) + 1 ) ; } else { counts . put ( s . charAt ( i ) , 1 ) ; } indices . add ( i ) ; } } if ( counts . size ( ) < 5 ) return - 1 ; int flag1 = 0 , flag2 = 0 ; int i = 0 ; int j = indices . size ( ) - 1 ; while ( ( j - i ) >= 4 ) { if ( flag1 == 0 && counts . get ( s . charAt ( indices . get ( i ) ) ) > 1 ) { counts . replace ( s . charAt ( indices . get ( i ) ) , counts . get ( s . charAt ( indices . get ( i ) ) ) - 1 ) ; i ++ ; } else flag1 = 1 ; if ( flag2 == 0 && counts . get ( s . charAt ( indices . get ( j ) ) ) > 1 ) { counts . replace ( s . charAt ( indices . get ( j ) ) , counts . get ( s . charAt ( indices . get ( j ) ) ) - 1 ) ; j -- ; } else flag2 = 1 ; if ( flag1 == 1 && flag2 == 1 ) break ; } return ( indices . get ( j ) - indices . get ( i ) + 1 ) ; } public static void main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; System . out . print ( findMinLength ( s ) ) ; } }
Longest Subsequence of a String containing only Consonants | Java program to find the longest subsequence which contain all consonants ; Returns true if x is consonants . ; Function to check whether a character is consonants or not ; Function to find the longest subsequence which contain all consonants ; Driver code ; Function call
class GFG { static boolean isConsonants ( char x ) { x = Character . toLowerCase ( x ) ; return ! ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static String longestConsonantsSubsequence ( String str ) { String answer = " " ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isConsonants ( str . charAt ( i ) ) ) { answer += str . charAt ( i ) ; } } return answer ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( longestConsonantsSubsequence ( str ) + "NEW_LINE"); } }
Find the length of the longest subsequence with first K alphabets having same frequency | Java program to find the longest subsequence with first K alphabets having same frequency ; Function to return the length of the longest subsequence with first K alphabets having same frequency ; Map to store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
import java . util . * ; class GFG { static int lengthOfSubsequence ( String str , int K ) { Map < Character , Integer > mp = new HashMap < > ( ) ; for ( char ch : str . toCharArray ( ) ) { mp . put ( ch , mp . getOrDefault ( ch , 0 ) + 1 ) ; } int minimum = mp . get ( ' A ' ) ; for ( int i = 1 ; i < K ; i ++ ) { minimum = Math . min ( minimum , mp . get ( ( char ) ( i + ' A ' ) ) ) ; } return minimum * K ; } public static void main ( String [ ] args ) { String str = " ACAABCCAB " ; int K = 3 ; System . out . println ( lengthOfSubsequence ( str , K ) ) ; } }
Detecting negative cycle using Floyd Warshall | Java Program to check if there is a negative weight cycle using Floyd Warshall Algorithm ; Number of vertices in the graph ; Define Infinite as a large enough value . This value will be used for vertices not connected to each other ; Returns true if graph has negative weight cycle else false . ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of vertices ; Initialize the solution matrix same as input graph matrix . Or we can say the initial values of shortest distances are based on shortest paths considering no intermediate vertex . ; Add all vertices one by one to the set of intermediate vertices . -- -> Before start of a iteration , we have shortest distances between all pairs of vertices such that the shortest distances consider only the vertices in set { 0 , 1 , 2 , . . k - 1 } as intermediate vertices . -- -- > After the end of a iteration , vertex no . k is added to the set of intermediate vertices and the set becomes { 0 , 1 , 2 , . . k } ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If distance of any verex from itself becomes negative , then there is a negative weight cycle . ; Driver code ; Let us create the following weighted graph 1 ( 0 ) -- -- -- -- -- -> ( 1 ) / | \ | | | - 1 | | - 1 | \ | / ( 3 ) < -- -- -- -- -- - ( 2 ) - 1
class GFG { static final int V = 4 ; static final int INF = 99999 ; static boolean negCyclefloydWarshall ( int graph [ ] [ ] ) { int dist [ ] [ ] = new int [ V ] [ V ] , i , j , k ; for ( i = 0 ; i < V ; i ++ ) for ( j = 0 ; j < V ; j ++ ) dist [ i ] [ j ] = graph [ i ] [ j ] ; for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } for ( i = 0 ; i < V ; i ++ ) if ( dist [ i ] [ i ] < 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , INF , INF } , { INF , 0 , - 1 , INF } , { INF , INF , 0 , - 1 } , { - 1 , INF , INF , 0 } } ; if ( negCyclefloydWarshall ( graph ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Digital Root of a given large integer using Recursion | Java program to print the digital root of a given very large number ; Function to convert given sum into String ; Loop to extract digit one by one from the given sum and concatenate into the String ; Type casting for concatenation ; Return converted String ; Function to get individual digit sum from String ; Loop to get individual digit sum ; Function call to convert sum into String ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print final digit
class GFG { static String convertToString ( int sum ) { String str = " " ; while ( sum > 0 ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } static String GetIndividulaDigitSum ( String str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str . charAt ( i ) - '0' ; } return convertToString ( sum ) ; } static int GetDigitalRoot ( String str ) { if ( str . length ( ) == 1 ) { return str . charAt ( 0 ) - '0' ; } str = GetIndividulaDigitSum ( str , str . length ( ) ) ; return GetDigitalRoot ( str ) ; } public static void main ( String [ ] args ) { String str = "675987890789756545689070986776987" ; System . out . print ( GetDigitalRoot ( str ) ) ; } }
Count the Number of matching characters in a pair of strings | Java code to count number of matching characters in a pair of strings ; Function to count the matching characters ; Traverse the string 1 char by char ; This will check if str1 [ i ] is present in str2 or not str2 . find ( str1 [ i ] ) returns - 1 if not found otherwise it returns the starting occurrence index of that character in str2 ; Driver code
class GFG { static void count ( String str1 , String str2 ) { int c = 0 , j = 0 ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str2 . indexOf ( str1 . charAt ( i ) ) >= 0 ) { c += 1 ; } } System . out . println ( " No . ▁ of ▁ matching ▁ characters ▁ are : ▁ " + c ) ; } public static void main ( String [ ] args ) { String str1 = " aabcddekll12 @ " ; String str2 = " bb2211@55k " ; count ( str1 , str2 ) ; } }
Check if count of Alphabets and count of Numbers are equal in the given String | Java program to check if the count of alphabets and numbers in a String are equal or not . ; Function to count the number of alphabets ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code
class GFG { static int countOfLetters ( String str ) { int letter = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) || ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) ) letter ++ ; } return letter ; } static int countOfNumbers ( String str ) { int number = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) number ++ ; } return number ; } static void check ( String str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } public static void main ( String [ ] args ) { String str = " GeeKs01324" ; check ( str ) ; } }
Minimum cost to traverse from one index to another in the String | Java implementation of the above approach . ; function to find the minimum cost ; graph ; adjacency matrix ; initialising adjacency matrix ; creating adjacency list ; pushing left adjacent elelemt for index ' k ' ; pushing right adjacent element for index ' k ' ; queue to perform BFS ; visited array ; variable to store depth of BFS ; BFS ; number of elements in the current level ; inner loop ; current element ; popping queue ; base case ; checking if the current node is required node ; iterating through the current node ; updating depth ; Driver Code ; input variables ; function to find the minimum cost
import java . util . * ; class GFG { static int findMinCost ( char [ ] s , int i , int j ) { Vector < Integer > [ ] gr = new Vector [ 26 ] ; for ( int iN = 0 ; iN < 26 ; iN ++ ) gr [ iN ] = new Vector < Integer > ( ) ; boolean [ ] [ ] edge = new boolean [ 26 ] [ 26 ] ; for ( int k = 0 ; k < 26 ; k ++ ) for ( int l = 0 ; l < 26 ; l ++ ) edge [ k ] [ l ] = false ; for ( int k = 0 ; k < s . length ; k ++ ) { if ( k - 1 >= 0 && ! edge [ s [ k ] - 97 ] [ s [ k - 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . add ( s [ k - 1 ] - 97 ) ; edge [ s [ k ] - 97 ] [ s [ k - 1 ] - 97 ] = true ; } if ( k + 1 <= s . length - 1 && ! edge [ s [ k ] - 97 ] [ s [ k + 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . add ( s [ k + 1 ] - 97 ) ; edge [ s [ k ] - 97 ] [ s [ k + 1 ] - 97 ] = true ; } } Queue < Integer > q = new LinkedList < Integer > ( ) ; q . add ( s [ i ] - 97 ) ; boolean [ ] v = new boolean [ 26 ] ; int d = 0 ; while ( q . size ( ) > 0 ) { int cnt = q . size ( ) ; while ( cnt -- > 0 ) { int curr = q . peek ( ) ; q . remove ( ) ; if ( v [ curr ] ) continue ; v [ curr ] = true ; if ( curr == s [ j ] - 97 ) return d ; for ( Integer it : gr [ curr ] ) q . add ( it ) ; } d ++ ; } return - 1 ; } public static void main ( String [ ] args ) { String s = " abcde " ; int i = 0 ; int j = 4 ; System . out . print ( findMinCost ( s . toCharArray ( ) , i , j ) ) ; } }
Index of character depending on frequency count in string | Java implementation of the approach ; Function to perform the queries ; L [ i ] [ j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i ] [ j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ ] [ ] and R [ ] [ ] as it is cnt [ j ] th occurrence of character k ; Only update L [ ] [ ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries
class GFG { static int MAX = 26 ; static void performQueries ( String str , int q , int type [ ] , char ch [ ] , int freq [ ] ) { int n = str . length ( ) ; int [ ] [ ] L = new int [ MAX ] [ n ] ; int [ ] [ ] F = new int [ MAX ] [ n ] ; int [ ] cnt = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = str . charAt ( i ) - ' a ' ; cnt [ k ] ++ ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j ] [ cnt [ j ] ] = i ; F [ j ] [ cnt [ j ] ] = i ; } else L [ j ] [ cnt [ j ] ] = L [ j ] [ cnt [ j ] ] + 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { System . out . print ( L [ ch [ i ] - ' a ' ] [ freq [ i ] ] ) ; } else { System . out . print ( F [ ch [ i ] - ' a ' ] [ freq [ i ] ] ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int type [ ] = { 1 , 2 } ; char ch [ ] = { ' e ' , ' k ' } ; int freq [ ] = { 2 , 2 } ; int q = type . length ; performQueries ( str , q , type , ch , freq ) ; } }
Number of index pairs such that s [ i ] and s [ j ] are anagrams | Java program to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call
import java . util . * ; class GFG { static int anagram_pairs ( String [ ] s , int n ) { Map < String , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char [ ] chArr = s [ i ] . toCharArray ( ) ; Arrays . sort ( chArr ) ; s [ i ] = new String ( chArr ) ; if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < String , Integer > i : mp . entrySet ( ) ) { int k = i . getValue ( ) ; ans += ( k * ( k - 1 ) ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { String [ ] s = { " aaab " , " aaba " , " baaa " , " cde " , " dec " } ; int n = s . length ; System . out . println ( anagram_pairs ( s , n ) ) ; } }
Insert a Character in a Rotated String | Java implementation of the approach ; Function to insert the character ; To store last position where the insertion is done ; To store size of the string ; To store the modified string ; To store characters ; Add first character to the string ; Update the size ; Update the index of last insertion ; Insert all other characters to the string ; Take the character ; Take substring upto ind ; Take modulo value of k with the size of the string ; Check if we need to move to the start of the string ; If we don 't need to move to  start of the string ; Take substring from upto temp ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; If we need to move to start of the string ; Take substring which will before the inserted character ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; Return the required character ; Driver code ; Function call
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 ) ) ; } }