Partition a set into two subsets such that difference between max of one and min of other is minimized | Function to split the array ; Sort the array in increasing order ; Calculating the max difference between consecutive elements ; Return the final minimum difference ; Given array arr [ ] ; Size of array ; Function Call
function splitArray ( arr , N ) { arr . sort ( ) ; let result = Number . MAX_VALUE ; for ( let i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } let arr = [ 3 , 1 , 2 , 6 , 4 ] ; let N = arr . length ; document . write ( splitArray ( arr , N ) ) ;