Longest sub | Function to find the starting and the ending index of the sub - array with equal number of alphabets and numeric digits ; If its an alphabet ; Else its a number ; Pick a starting poas i ; Consider all sub - arrays starting from i ; If this is a 0 sum sub - array then compare it with maximum size sub - array calculated so far ; If no valid sub - array found ; Driver code
def findSubArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE maxsize = - 1 NEW_LINE startindex = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] . isalpha ( ) ) : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if arr [ i ] == '1' : NEW_LINE INDENT sum = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum = - 1 NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ j ] == 0 : NEW_LINE INDENT sum -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT if ( sum == 0 and maxsize < j - i + 1 ) : NEW_LINE INDENT maxsize = j - i + 1 NEW_LINE startindex = i NEW_LINE DEDENT DEDENT DEDENT if ( maxsize == - 1 ) : NEW_LINE INDENT print ( maxsize , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( startindex , ( startindex + maxsize - 1 ) ) NEW_LINE DEDENT DEDENT arr = [ ' A ' , ' B ' , ' X ' , '4' , '6' , ' X ' , ' a ' ] NEW_LINE size = len ( arr ) NEW_LINE findSubArray ( arr , size ) NEW_LINE