Split array into two subarrays such that difference of their maximum is minimum | C ++ Program to split a given array such that the difference between their maximums is minimized . ; Sort the array ; Return the difference between two highest elements ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDif ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } int main ( ) { int arr [ ] = { 7 , 9 , 5 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinDif ( arr , N ) ; return 0 ; }