Minimize length of an array by removing similar subarrays from both ends | Function to minimize length of the array by removing similar subarrays from both ends of the array ; Initialize two pointers ; Stores the current integer ; Check if the elements at both ends are same or not ; Move the front pointer ; Move the rear pointer ; Print the minimized length of the array ; Input ; Function call to find the minimized length of the array
function findMinLength ( arr , N ) { let front = 0 , back = N - 1 ; while ( front < back ) { let x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } document . write ( back - front + 1 ) ; document . write ( " " ) ; } let arr = [ 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 ] ; let N = arr . length ; findMinLength ( arr , N ) ;