Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:27:18+00:00 2026-05-23T09:27:18+00:00

Given an array of elements find the largest possible number that can be formed

  • 0

Given an array of elements find the largest possible number that can
be formed by using the elements of the array.
eg: 10 9
ans: 910
2 3 5 78
ans: 78532
100 9
ans: 9100

I know this problem has a solution using customized string comparator but i dont understand how it really works.

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>

    using namespace std;


    bool compare ( string a, string b )  
    {  
            return atoi( (a+b).c_str() ) < atoi((b+a).c_str() );  
    }

    int main()  
    {  
            vector<string> vs;  
            string s;  
            while ( cin >> s ) {  
                    vs.push_back(s);  
            }  
            sort( vs.begin(), vs.end(), compare );  
            for ( int i = vs.size()-1; i >= 0; i-- ) {  
                    cout << vs[i];  
            }  
    }   

Can anyone propose an algorithm to solve this problem?
Explanation of the above comparator will be appreciated.
Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:27:19+00:00Added an answer on May 23, 2026 at 9:27 am

    Indeed, if we have two strings S and T, we will most commonly concatenate them in lexicographically reverse order, to make the biggest digits come forward. However, this approach doesn’t perfectly work, when one of these strings is a prefix for the other one.
    Let T = SA, i.e. S is a prefix of T. We have two choices of concatenation: SSA and SAS. Obviously our choice will depend on which number is bigger: AS or SA.
    Below is the modification of your code which satisfies this algo:

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    
    bool compare ( string a, string b )  
    {  
            int i, j;
            for( i = 0; i < a.size() && i < b.size(); ++i )
                   if( a[ i ] != b[ i ] )
                         break;
            if( i < a.size() && i < b.size() ) //  if digit mismatch happened
                   return a[ i ] < b[ i ];
            if( i == a.size() )                // a is a prefix for b
            {
                   string suffix = b.substr( i );
                   return a + suffix < suffix + a;
            }
            else                               // b is a prefix for a
            {
                   string suffix = a.substr( i );
                   return suffix + b < b + suffix;
            }
    }
    
    int main()  
    {  
            vector<string> vs;  
            string s;  
            while ( cin >> s ) {  
                    vs.push_back(s);  
            }  
            sort( vs.begin(), vs.end(), compare );  
            for ( int i = vs.size()-1; i >= 0; i-- ) {  
                    cout << vs[i];  
            }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array. How can we find sum of elements in index interval (i,
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
Question: Given a sorted array A find all possible difference of elements from A.
how can we find out different combination of the elements of an array using
Is there a way to select all elements that have a given style using
Given an array of integers, how can you find two indices, i and j,
Suppose that you're given an array A of n distinct elements drawn from some
Possible Duplicate: Given two arrays a and b .Find all pairs of elements (a1,b1)
Given an array of integers ,You have to find two elements whose XOR is
Given an array with N elements. We know that one of those elements repeats

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.