Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printMiddleString ( string S , string T , int N ) { vector < int > a1 ( N + 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = S [ i ] - ' a ' + T [ i ] - ' a ' ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( a1 [ i ] & 1 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] /= 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { cout << char ( a1 [ i ] + ' a ' ) ; } return 0 ; } int main ( ) { int N = 5 ; string S = " afogk " ; string T = " asdji " ; printMiddleString ( S , T , N ) ; }