Skip to content
Related Articles

Related Articles

Php Program to Arrange given numbers to form the biggest number

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 22 Dec, 2021

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 simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.

So how do we go about it? The idea is to use any comparison based sorting algorithm. 
In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. 

Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.

Following is the implementation of the above approach. 
To keep the code simple, numbers are considered as strings, the vector is used instead of a normal array. 

Below is the implementation of the above approach: 

PHP




<?php
// Given an array of numbers, program 
// to arrange the numbers to form the
// largest number 
  
// A comparison function which is used
// by sort() in printLargest() 
function myCompare($X, $Y
    // First append Y at the end of X 
    $XY = $Y.$X
      
    // Then append X at the end of Y 
    $YX = $X.$Y
      
    // Now see which of the two formed
    // numbers is greater 
    return strcmp($XY, $YX) > 0 ? 1: 0; 
  
// The main function that prints the
// arrangement with the largest value. 
// The function accepts a vector of strings 
function printLargest($arr
    // Sort the numbers using library sort 
    // function. The function uses our 
    // comparison function myCompare() to 
    // compare two strings. 
    usort($arr, "myCompare"); 
  
    for ($i = 0; $i < count($arr) ; $i++ ) 
        echo $arr[$i]; 
  
// Driver Code
$arr = array("54", "546", "548", "60"); 
printLargest($arr); 
  
// This code is contributed by rathbhupendra
?>

Output:

6054854654

Time Complexity:  O(nlogn) ,sorting is considered to have running time complexity of O(nlogn) and the for loop runs in O(n) time.
Auxiliary Space: O(1).

Please refer complete article on Arrange given numbers to form the biggest number | Set 1 for more details!


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!