# Maximum possible number with concatenations of K numbers from given array

• Last Updated : 02 Jun, 2022

Given an array arr[] of N integers and a positive integer K, the task is to choose K integers from the array arr[] such that concatenation of them forms the largest possible integer. All the array elements must be chosen at least once for creating the number.

Note: It is always guaranteed that N is greater than or equal to K.

Examples:

Input: arr[] = {3, 2, 7}, K = 3
Output: 732
Explanation:
Since each array element has to be used at least once, the biggest possible number is 732.

Input: arr[] = {1, 10, 100}, K = 4
Output: 110100100

Approach: The above problem can be solved by sorting and converting numbers to strings. The optimal approach is to take all numbers once. After that, take the number with the most digits. In case of a tie, take the lexicographically largest number. Convert all the numbers in the string using the to_string() function. Follow the below steps to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Custom comparator function``bool` `str_cmp(string s1, string s2)``{``    ``return` `(s1 + s2 < s2 + s1);``}` `// Function to get the largest possible``// string``string largestNumber(vector<``int``> arr,``                     ``int` `K)``{``    ``int` `N = arr.size();` `    ``// Initialize a new variable which``    ``// will store the answers.``    ``string res = ``""``;` `    ``// Sort the numbers array in``    ``// non-decreasing order``    ``sort(arr.begin(), arr.end());` `    ``// Stores the array element which will``    ``// be used to construct the answer``    ``vector v;` `    ``// Take all numbers atleast once``    ``for` `(``int` `i = 0; i < N; i++) {``        ``v.push_back(to_string(arr[i]));``    ``}``    ``K -= N;` `    ``// Take the largest digits number``    ``// for remaining required numbers``    ``while` `(K) {``        ``v.push_back(to_string(arr[N - 1]));``        ``K--;``    ``}` `    ``// Sort the final answer according to``    ``// the comparator function.``    ``sort(v.begin(), v.end(), str_cmp);``    ``for` `(``int` `i = v.size() - 1; i >= 0; i--)``        ``res += v[i];` `    ``return` `res;``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 1, 10, 100 };``    ``int` `K = 4;``    ``cout << largestNumber(arr, K);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.io.*;``import` `java.lang.*;``import` `java.util.*;` `class` `GFG``{``  ` `    ``// Custom comparator function``    ``static` `int` `str_cmp(String s1, String s2)``    ``{``        ``return` `(s1 + s2).compareTo(s2 + s1);``        ` `    ``}``    ` `  ``// Function to get the largest possible``  ``// String``  ``static` `String largestNumber(``int` `arr[], ``int` `K)``  ``{``    ``int` `N = arr.length;`` ` `    ``// Initialize a new variable which``    ``// will store the answers.``    ``String res = ``""``;`` ` `    ``// Sort the numbers array in``    ``// non-decreasing order``    ``Arrays.sort(arr);`` ` `    ``// Stores the array element which will``    ``// be used to construct the answer``    ``ArrayList v = ``new` `ArrayList();`` ` `    ``// Take all numbers atleast once``    ``for` `(``int` `i = ``0``; i < N; i++) {``      ``v.add(String.join(``""``,Integer.toString(arr[i])));``    ``}``    ``K -= N;`` ` `    ``// Take the largest digits number``    ``// for remaining required numbers``    ``while` `(K > ``0``) {``      ``v.add(String.join(``""``,Integer.toString(arr[N - ``1``])));``      ``K--;``    ``}`` ` `    ``// Sort the readonly answer according to``    ``// the comparator function.``    ``v.sort((s1,s2) -> str_cmp(s1,s2));``    ``for` `(``int` `i = v.size() - ``1``; i >= ``0``; i--)``      ``res += v.get(i);`` ` `    ``return` `res;``  ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args) {``        ` `    ``int` `arr[] = { ``1``, ``10``, ``100` `};``    ``int` `K = ``4``;``    ``System.out.print(largestNumber(arr, K));`` ` `    ``}``}` `// This code is contributed by Pushpesh Raj.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `public` `class` `GFG``{` `  ``// Custom comparator function` `  ``// Function to get the largest possible``  ``// String``  ``static` `String largestNumber(``int``[] arr, ``int` `K)``  ``{``    ``int` `N = arr.Length;` `    ``// Initialize a new variable which``    ``// will store the answers.``    ``String res = ``""``;` `    ``// Sort the numbers array in``    ``// non-decreasing order``    ``Array.Sort(arr);` `    ``// Stores the array element which will``    ``// be used to construct the answer``    ``List v = ``new` `List();` `    ``// Take all numbers atleast once``    ``for` `(``int` `i = 0; i < N; i++) {``      ``v.Add(String.Join(``""``,arr[i]));``    ``}``    ``K -= N;` `    ``// Take the largest digits number``    ``// for remaining required numbers``    ``while` `(K > 0) {``      ``v.Add(String.Join(``""``,arr[N - 1]));``      ``K--;``    ``}` `    ``// Sort the readonly answer according to``    ``// the comparator function.``    ``v.Sort((s1,s2) => (s1 + s2).CompareTo(s2 + s1));``    ``for` `(``int` `i = v.Count - 1; i >= 0; i--)``      ``res += v[i];` `    ``return` `res;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args) {``    ``int``[] arr = { 1, 10, 100 };``    ``int` `K = 4;``    ``Console.Write(largestNumber(arr, K));` `  ``}``}` `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output:

`110100100`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up