Check if sequence of removed middle elements from an array is sorted or not | Java program for the above approach ; Function to check if sequence of removed middle elements from an array is sorted or not ; Points to the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
import java . util . * ; class GFG { static boolean isSortedArray ( int [ ] arr , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 3 , 1 , 2 , 5 } ; int N = arr . length ; if ( isSortedArray ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }