Arrange given numbers to form the biggest number
Read full article from Arrange given numbers to form the biggest number | GeeksforGeeks
Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.
// A comparison function which is used by sort() in printLargest()int myCompare(string X, string Y){ // first append Y at the end of X string XY = X.append(Y); // then append X at the end of Y string YX = Y.append(X); // Now see which of the two formed numbers is greater return XY.compare(YX) > 0 ? 1: 0;}// The main function that prints the arrangement with the largest value.// The function accepts a vector of stringsvoid printLargest(vector<string> arr){ // Sort the numbers using library sort funtion. The function uses // our comparison function myCompare() to compare two strings. // See http://www.cplusplus.com/reference/algorithm/sort/ for details sort(arr.begin(), arr.end(), myCompare); for (int i=0; i < arr.size() ; i++ ) cout << arr[i];}