Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | Java Program for the above approach ; Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code
import java . util . * ; class GFG { static void printMiddleString ( String S , String T , int N ) { int [ ] a1 = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = ( int ) S . charAt ( i ) - 97 + ( int ) T . charAt ( i ) - 97 ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += ( int ) a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( ( a1 [ i ] & 1 ) != 0 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] = ( int ) a1 [ i ] / 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( ( char ) ( a1 [ i ] + 97 ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; String S = " afogk " ; String T = " asdji " ; printMiddleString ( S , T , N ) ; } }