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
function minLength ( A , N ) { var elem = A [ 0 ] , count = 1 ; for ( var i = 1 ; i < N ; i ++ ) { if ( A [ i ] == elem ) { count ++ ; } else { break ; } } if ( count == N ) return N ; else return 1 ; } var arr = [ 2 , 1 , 3 , 1 ] ; var N = arr . length ; document . write ( minLength ( arr , N ) + " " ) ;