Maximum possible number with concatenations of K numbers from given array
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 their 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++
#include <bits/stdc++.h>
using namespace std;
bool str_cmp(string s1, string s2)
{
return (s1 + s2 < s2 + s1);
}
string largestNumber(vector< int > arr,
int K)
{
int N = arr.size();
string res = "" ;
sort(arr.begin(), arr.end());
vector<string> v;
for ( int i = 0; i < N; i++) {
v.push_back(to_string(arr[i]));
}
K -= N;
while (K) {
v.push_back(to_string(arr[N - 1]));
K--;
}
sort(v.begin(), v.end(), str_cmp);
for ( int i = v.size() - 1; i >= 0; i--)
res += v[i];
return res;
}
int main()
{
vector< int > arr = { 1, 10, 100 };
int K = 4;
cout << largestNumber(arr, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int str_cmp(String s1, String s2)
{
return (s1 + s2).compareTo(s2 + s1);
}
static String largestNumber( int arr[], int K)
{
int N = arr.length;
String res = "" ;
Arrays.sort(arr);
ArrayList<String> v = new ArrayList<String>();
for ( int i = 0 ; i < N; i++) {
v.add(String.join( "" ,Integer.toString(arr[i])));
}
K -= N;
while (K > 0 ) {
v.add(String.join( "" ,Integer.toString(arr[N - 1 ])));
K--;
}
v.sort((s1,s2) -> str_cmp(s1,s2));
for ( int i = v.size() - 1 ; i >= 0 ; i--)
res += v.get(i);
return res;
}
public static void main(String[] args) {
int arr[] = { 1 , 10 , 100 };
int K = 4 ;
System.out.print(largestNumber(arr, K));
}
}
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static String largestNumber( int [] arr, int K)
{
int N = arr.Length;
String res = "" ;
Array.Sort(arr);
List<String> v = new List<String>();
for ( int i = 0; i < N; i++) {
v.Add(String.Join( "" ,arr[i]));
}
K -= N;
while (K > 0) {
v.Add(String.Join( "" ,arr[N - 1]));
K--;
}
v.Sort((s1,s2) => (s1 + s2).CompareTo(s2 + s1));
for ( int i = v.Count - 1; i >= 0; i--)
res += v[i];
return res;
}
public static void Main(String[] args) {
int [] arr = { 1, 10, 100 };
int K = 4;
Console.Write(largestNumber(arr, K));
}
}
|
Javascript
<script>
function largestNumber(arr,
K) {
let N = arr.length;
let res = "" ;
arr.sort( function (a, b) { return a - b })
let v = [];
for (let i = 0; i < N; i++) {
v.push(arr[i].toString());
}
K -= N;
while (K) {
v.push(arr[N - 1].toString());
K--;
}
v.sort( function (s1, s2) { return parseInt(s1 + s2) - parseInt(s2 + s1) });
for (let i = v.length - 1; i >= 0; i--)
res += v[i];
return res;
}
let arr = [1, 10, 100];
let K = 4;
document.write(largestNumber(arr, K));
</script>
|
Python3
def largestNumber(arr, K):
res = ""
arr.sort()
v = []
for i in range ( len (arr)):
v.append( str (arr[i]))
K - = len (arr)
while K > 0 :
v.append( str (arr[ - 1 ]))
K - = 1
v.sort(key = lambda x: x * 3 , reverse = True )
res = ''.join(v)
return res
arr = [ 1 , 10 , 100 ]
K = 4
print (largestNumber(arr, K))
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Last Updated :
22 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...