Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum | Function that returns the minimum length of the array after merging unequal adjacent elements ; Stores the first element and its frequency ; Traverse the array ; If all elements are equal ; No merge - pair operations can be performed ; Otherwise ; Given array ; Length of the array ; Function call
def minLength ( A , N ) : NEW_LINE INDENT elem = A [ 0 ] NEW_LINE count = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( A [ i ] == elem ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( count == N ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT arr = [ 2 , 1 , 3 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minLength ( arr , N ) ) NEW_LINE