Count of only repeated element in a sorted array of consecutive elements | Assumptions : vector a is sorted , max - difference of two adjacent elements is 1 ; if a [ m ] = m + a [ 0 ] , there is no repeating character in [ s . . m ] ; if a [ m ] < m + a [ 0 ] , there is a repeating character in [ s . . m ] ; Driver code
def sequence ( a ) : NEW_LINE INDENT if ( len ( a ) == 0 ) : NEW_LINE INDENT return [ 0 , 0 ] NEW_LINE DEDENT s = 0 NEW_LINE e = len ( a ) - 1 NEW_LINE while ( s < e ) : NEW_LINE INDENT m = ( s + e ) // 2 NEW_LINE if ( a [ m ] >= m + a [ 0 ] ) : NEW_LINE INDENT s = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT e = m NEW_LINE DEDENT DEDENT return [ a [ s ] , len ( a ) - ( a [ len ( a ) - 1 ] - a [ 0 ] ) ] NEW_LINE DEDENT p = sequence ( [ 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 ] ) NEW_LINE print ( " Repeated ▁ element ▁ is " , p [ 0 ] , " , ▁ it ▁ appears " , p [ 1 ] , " times " ) NEW_LINE