Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set
if ( ( $ a [ $ i ] == $ a [ $ i + 1 ] or $ a [ $ i ] - $ a [ $ i + 1 ] == 1 ) and ( ! $ flag ) ) {