Minimum number of colors required to color a Circular Array | Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call
def colorRequired ( arr , n ) : NEW_LINE INDENT all_same = True NEW_LINE one_adjacent_same = False NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT all_same = False NEW_LINE DEDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT one_adjacent_same = True NEW_LINE DEDENT DEDENT if ( all_same == True ) : NEW_LINE INDENT print ( 1 ) NEW_LINE return NEW_LINE DEDENT if ( n % 2 == 0 or one_adjacent_same == True ) : NEW_LINE INDENT print ( 2 ) NEW_LINE return NEW_LINE DEDENT print ( 3 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE colorRequired ( arr , n ) NEW_LINE DEDENT