Given an array of non-negative numbers(of Integer Range), they are needed to be arranged in some order such that it gives the max number. For example given array is A[1, 34, 3, 98, 9, 76, 45, 4, 12, 121]. if we arrange these numbers in the following order, A[9, 98, 76, 45, 4, 34, 3, 12, 121, 1], then by joining them we get “99876454343121211” as largest number.
Examples:
Input : [1, 34, 3, 98, 9, 76, 45, 4, 12, 121] Output : 99876454343121211 Input : [12, 121] Output : 12121
In SET 1, we have discussed an approach to do same. In this set, we will discuss another logic.
1) Find number of digits in the largest number. Let number of digits be n.
2) Create extended version of all numbers. In extended version, we have n+1 digits formed by concatenating the number of with itself and truncating extra digits.
3) Sort original numbers according to their extended values.
4) Concatenating the sorted numbers produces th required result.
Java
// Java program to arrange the numbers to form the // largest number import java.math.BigInteger; import java.util.*; public class LargestNumber { // method that returns largest number form public static String largestNumber(List<Integer> arr) { // finding number of digits in maximum element // present in array int n = Collections.max(arr).toString().length(); ArrayList<ExtendedNum> en = new ArrayList<ExtendedNum>(); for (int i = 0 ; i < arr.size(); i++) en.add(new ExtendedNum(arr.get(i), n)); // sort based on modified value Collections.sort(en, (p1, p2) -> (int)(p2.modifiedValue - p1.modifiedValue)); StringBuilder sb = new StringBuilder(); for (int i = 0; i < en.size(); i++) sb.append(new StringBuilder (Long.toString(en.get(i).originalValue))); // To remove any zeroes at head. BigInteger bi = new BigInteger(sb.toString()); return bi.toString(); } // Driver method public static void main(String[] args) { Integer arr[] = {1, 34, 3, 98, 9, 76, 45, 4, 12, 121}; List<Integer> l = Arrays.asList(arr); System.out.println(largestNumber(l)); } } // A utility class to generate new value class ExtendedNum { int originalValue; long modifiedValue; public ExtendedNum(int originalValue, int n) { this.originalValue = originalValue; String s = Integer.toString(originalValue); StringBuilder sb = new StringBuilder(s); StringBuilder ans = new StringBuilder(); while (ans.length() <= n + 1) ans.append(sb); s = ans.toString().substring(0, n + 1); modifiedValue = Long.parseLong(s); } public String toString() { return "[" + modifiedValue + ", " + originalValue + "]"; } } |
Python
# Python program to find largest # number from the given values # function that return largest # possible number def largestNumber(array): # extval is a empty list for extended # values and ans for calculating answer extval, ans = [], "" # calculating the length of largest number # from given and add 1 for further operation l = len(str(max(array))) + 1 # iterate given values and # calculating extended values for i in array: temp = str(i) * l # make tuple of extended value and # equivalant original value then # append to list extval.append((temp[:l:], i)) # sort extval in descending order extval.sort(reverse = True) # iterate extended values for i in extval: # concatinate original value equivalant # to extended value into ans variable ans += str(i[1]) if int(ans)==0: return "0" return ans # Driver code a = [1, 34, 3, 98, 9, 76, 45, 4, 12, 121] print(largestNumber(a)) # This code is contributed by Chhekur |
Output:
99876454343121211
This article is contributed by Kondiparthi Shanmukha Sarath. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Arrange given numbers to form the biggest number | Set 1
- Arrange numbers to form a valid sequence
- Arrange the numbers in the Array as per given inequalities
- Arrange the array such that upon performing given operations an increasing order is obtained
- Biggest integer which has maximum digit sum in range from 1 to n
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Reduce a given number to form a key by the given operations
- Form smallest number using indices of numbers chosen from Array with sum less than S
- Form minimum number from given sequence
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Count of subsequences of length 4 in form (x, x, x+1, x+1) | Set 2
- Rearrange an array in maximum minimum form | Set 2 (O(1) extra space)
- Rearrange an array in maximum minimum form | Set 1
- Count numbers from a given range that contains a given number as the suffix
- Print elements that can be added to form a given sum
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Print a given matrix in spiral form using direction tracking method
- Count of elements which form a loop in an Array according to given constraints
- Minimum count of increment of K size subarrays required to form a given Array


