Sorting both the ArrayLists
Collections . sort ( V ) ; Collections . sort ( V1 ) ; int maximum = Math . max ( ( V . get ( V . size ( ) - 1 ) - V . get ( 0 ) ) , ( V1 . get ( V1 . size ( ) - 1 ) - V1 . get ( 0 ) ) ) ; System . out . println ( maximum ) ; }