Find Largest number from set of given numbers ?
- Find number of digits in the largest number. Let us assume number of digits be n.
- Create extended version of all numbers. In extended version, we have n+1 digit formed by concatenating the number of with itself and truncating extra digits.
- Sorting the value from original numbers according to their extended values.
- Concatenating to the sorted numbers produce the required result.
Sample Code in C++
/ Given an array of numbers, program to arrange the numbers to form the
// largest number
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// A comparison function which is used by sort() in printLargest()
int myCompare(string X, string Y)
{
// first append Y at the end of X
string XY = X.append(Y);
// then append X at the end of Y
string YX = Y.append(X);
// Now see which of the two formed numbers is greater
return XY.compare(YX) > 0 ? 1: 0;
}
// The main function that prints the arrangement with the largest value.
// The function accepts a vector of strings
void printLargest(vector<string> arr)
{
sort(arr.begin(), arr.end(), myCompare);
for (int i=0; i < arr.size() ; i++ )
cout << arr[i];
}
// driver program to test above functions
int main()
{
vector<string> arr;
//output should be 5545727825
arr.push_back("25");
arr.push_back("457");
arr.push_back("278");
arr.push_back("55");
printLargest(arr);
return 0;
}
Output
Categorized in:
Tagged in:
Accenture interview questions and answers, Applied Materials interview questions and answers, arrange given numbers to form the biggest number c#, arrange given numbers to form the biggest number in c++, arrange given numbers to form the biggest number in python, arrange given numbers to form the biggest number java, arrange given numbers to form the biggest number javascript, arrange the numbers to form the biggest number, Atos interview questions and answers, biggest number ever, biggest numbers list, biggest possible number, BMC Software interview questions and answers, Bosch India Software interview questions and answers, CASTING NETWORKS INDIA PVT LIMITED interview questions and answers, Chetu interview questions and answers, Ciena Corporation interview questions and answers, Dell International Services India Pvt Ltd interview questions and answers, eInfochips interview questions and answers, Electronics Arts Inc interview questions and answers, find largest number possible by rearranging the number., Flipkart interview questions and answers, Harman International interview questions and answers, Indecomm Global Services interview questions and answers, infrrd interview questions and answers, largest concatenated number in c, largest number formed from an array in c++, largest possible number with digits in c, Larsen & Toubro interview questions and answers, Mathworks India Private Limited interview questions and answers, Mavenir interview questions and answers, Mphasis interview questions and answers, NetApp interview questions and answers, Oracle Corporation interview questions and answers, PeopleStrong interview questions and answers, Philips Software Centre Pvt Ltd interview questions and answers, SRM Technologies interview questions and answers, Symphony Teleca interview questions and answers, Tech Mahindra interview questions and answers, Tecnotree interview questions and answers, the biggest number, what is the biggest four digit number, what is the biggest number, what is the biggest number besides infinity, what is the biggest number ever, what is the biggest number possibleis google the biggest number, what is the biggest number with three 3's, what's the biggest number, which is the biggest number, which number is the biggest, Wipro Infotech interview questions and answers, Wipro interview questions and answers, write a program to arrange them such that they form the largest number., Yash Technologies interview questions and answers