Minimize length of an array by removing similar subarrays from both ends | Java program for the above approach ; 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 ; Driver Code ; Input ; Function call to find the minimized length of the array
import java . util . * ; class GFG { static void findMinLength ( int arr [ ] , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } System . out . println ( back - front + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = arr . length ; findMinLength ( arr , N ) ; } }