Partition a set into two subsets such that difference between max of one and min of other is minimized | java program for the above approach ; Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int splitArray ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int result = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . length ; System . out . print ( splitArray ( arr , N ) ) ; } }