Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximum possible number with concatenations of K numbers from given array

  • Last Updated : 06 Oct, 2021

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 <bits/stdc++.h>
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<string> 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;
}

Javascript




<script>
      // JavaScript Program to implement
      // the above approach
 
      // Function to get the largest possible
      // string
      function largestNumber(arr,
          K) {
          let N = arr.length;
 
          // Initialize a new variable which
          // will store the answers.
          let res = "";
          // Sort the numbers array in
          // non-decreasing order
          arr.sort(function (a, b) { return a - b })
 
          // Stores the array element which will
          // be used to construct the answer
          let v = [];
 
          // Take all numbers atleast once
          for (let i = 0; i < N; i++) {
              v.push(arr[i].toString());
          }
          K -= N;
 
          // Take the largest digits number
          // for remaining required numbers
          while (K) {
              v.push(arr[N - 1].toString());
              K--;
          }
 
          // Sort the final answer according to
          // the comparator function.
          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;
      }
 
      // Driver Code
 
      let arr = [1, 10, 100];
      let K = 4;
      document.write(largestNumber(arr, K));
 
 
     // This code is contributed by Potta Lokesh
 
  </script>

 
 

Output: 
110100100

 

 

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

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!