Arrange given numbers to form the biggest number
Given an array of numbers, arrange them in a way that yields the largest value. For example, if the given numbers are {54, 546, 548, 60}, the arrangement 6054854654 gives the largest value. And if the given numbers are {1, 34, 3, 98, 9, 76, 45, 4}, then the arrangement 998764543431 gives the largest value.
A simple solution that comes to our mind is to sort all numbers in descending order, but simply sorting doesn’t work. For example, 548 is greater than 60, but in output 60 comes before 548. As a second example, 98 is greater than 9, but 9 comes before 98 in output.
So how do we go about it? The idea is to use any comparison based sorting algorithm. In the used sorting algorithm, instead of using the default comparison, write a comparison function myCompare() and use it to sort numbers. Given two numbers X and Y, how should myCompare() decide which number to put first – we compare two numbers XY (Y appended at the end of X) and YX (X appended at the end of Y). If XY is larger, then X should come before Y in output, else Y should come before. For example, let X and Y be 542 and 60. To compare X and Y, we compare 54260 and 60542. Since 60542 is greater than 54260, we put Y first.
Following is C++ implementation of the above approach. To keep the code simple, numbers are considered as strings, and vector is used instead of normal array.
// 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 the numbers using library sort funtion. The function uses
// our comparison function myCompare() to compare two strings.
// See http://www.cplusplus.com/reference/algorithm/sort/ for details
sort(arr.begin(), arr.end(), myCompare);
for (int i=0; i < arr.size() ; i++ )
cout << arr[i];
}
// driverr program to test above functions
int main()
{
vector<string> arr;
//output should be 6054854654
arr.push_back("54");
arr.push_back("546");
arr.push_back("548");
arr.push_back("60");
printLargest(arr);
// output should be 777776
/*arr.push_back("7");
arr.push_back("776");
arr.push_back("7");
arr.push_back("7");*/
//output should be 998764543431
/*arr.push_back("1");
arr.push_back("34");
arr.push_back("3");
arr.push_back("98");
arr.push_back("9");
arr.push_back("76");
arr.push_back("45");
arr.push_back("4");
*/
return 0;
}
Output:
6054854654
This article is compiled by Ravi Chandra Enaganti. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
here's a simple solution.....
we scan d array and pick out the elemnts with greatest dgreatest digit on the left....
so if we take the array [1, 34, 3, 98, 9, 76, 45, 4]
the elements to be picked will be,9 and 98
among 9 and 98 the elemnt to b placed in d greates position can be decided by forming concatenation of XY and YX as given in explanation and found out.....
den the largest and secnd largst positioned elemnts can be placed on the 1st and 2nd position f d array by swapping dem wd d elemnts in dose 1st and secnd positions....
This process will again continue from start+2'th position as the first 2 positions already contain the largest possible elemnts.....
this process continues until the array is exhausted....
sorry...
a lil typo missd....after the swapping of 9 and 98 wuth d 0'th and 1'st position f d array we incremnt the position for the process to start by 2(start1=start+2) because we have already decided wr to place d 2 elemnts(9 and 98) out f d n elements in d aray....
in the next iteratn if there is only 1 elemt with the largest left didgit value then we will swap it with the 3 rd position f d array nd start the process from Start1+1(start +3).................so depending on d number of already decided position of elemnts we increment.
In C. Compiles without any errors on GCC 4.4.3-4.
Compiler options used: -Wall.
#define CH_MAX 10 #include <stdio.h> #include <stdlib.h> #include <string.h> void arrangeForLargest(int[], int); int funkyCompare(const void *, const void *); int main(int argc, char *argv[]) { int r[] = {54, 546, 548, 60}; /* replace this with an array of your choice */ arrangeForLargest(r, sizeof(r) / sizeof(r[0])); return 0; } int funkyCompare(const void *x, const void *y) { char *s = malloc(CH_MAX * sizeof(char) + 1); char *t = malloc(CH_MAX * sizeof(char) + 1); sprintf(s, "%d", * (int*) x); sprintf(t, "%d", * (int*) y); int a = strlen(s) + strlen(t); char *v = malloc(a * sizeof(char) + 1); char *w = malloc(a * sizeof(char) + 1); strcpy(v, s); strcat(v, t); strcpy(w, t); strcat(w, s); return strcmp(v, w) >= 0 ? 0 : 1; } void arrangeForLargest(int r[], int s) { int a; qsort(r, s, sizeof(int), funkyCompare); for(a = 0; a < s; ++a) printf("%d", r[a]); printf("\n"); }/* //This program #include<stdio.h> main() { int n=5,a[n],i,b[n],max=0,val,pos=0,k=0; printf("Enter the five Number "); for(i=0;i<n;i++) scanf(" %d" ,&a[i]); for(i=0;i<n;i++) { b[i]=length(a[i]); } max=maxarray(b,n); for(i=0;i<n;i++) { b[i]=max-b[i]; } for(i=0;i<n;i++) { val=b[i]; b[i]=a[i]; while(val!=0) { a[i]=a[i]*10; val--; } } printf("\nMaximum Number from given array=>\t"); while(k!=5) { max=0; for(i=0;i<n;i++) { if(a[i]>max) { max=a[i]; pos=i; } } a[pos]=0; printf("%d",b[pos]); k++; } printf("\n"); } int length(int n) //It will find the length of all array element { int i=0; while(n!=0) { ++i; n=n/10; } return i; } int maxarray(int b[],int n) // It will find the length of max array element { int i=0; int max=b[i]; for(i=1;i<n;i++) { if(b[i]>max) max=b[i]; } return max; } */#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> void swap ( int* a, int* b ) { int t = *a; *a = *b; *b = t; } int pivote(int a[],int start,int end){ int p = a[end]; int i=start,j=start-1; char b[5]; char pi[5]; char str1[6]; char str2[6]; memset (pi,'\0',sizeof(b)); itoa(p,pi,10); for(i=start;i<=end-1;i++){ memset (b,'\0',sizeof(b)); memset (str1,'\0',sizeof(str1)); memset (str2,'\0',sizeof(str2)); itoa(a[i],b,10); strcat(str1,pi); strcat(str1,b); strcat(str2,b); strcat(str2,pi); if(strcmp(str2,str1)>=0){ j++; swap(&a[i],&a[j]); } } swap(&a[j+1],&a[end]); return j+1; } void findmax(int a[],int start,int end){ if(start>end){ return; } int p= pivote(a,start,end); findmax(a,start,p-1); findmax(a,p+1,end); } int main(){ int a[] = {54,546,548,60}; int size = sizeof(a)/sizeof(a[0]); findmax(a,0,size-1); for(int i=0;i<size;i++){ printf("%d",a[i]); } getch(); }// 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; vector<string> store; int len = 0; string res = ""; void printLargest(int mask,string temp) { if(mask == 0) { res = max(res,temp); return; } for(int i=0; i<len; i++) { if(mask & (1<<i)) { printLargest(mask ^(1<<i),temp+store[i]); } } return; } // driverr program to test above functions int main() { vector<string> arr; //output should be 6054854654 arr.push_back("54"); arr.push_back("546"); arr.push_back("548"); arr.push_back("60"); store = arr; len = arr.size(); printLargest((1<<len)-1,""); printf("%s", res); arr.clear(); res.clear(); // output should be 777776 arr.push_back("7"); arr.push_back("776"); arr.push_back("7"); arr.push_back("7"); store = arr; len = arr.size(); printLargest((1<<len)-1,""); printf("%s", res); arr.clear(); res.clear(); //output should be 998764543431 arr.push_back("1"); arr.push_back("34"); arr.push_back("3"); arr.push_back("98"); arr.push_back("9"); arr.push_back("76"); arr.push_back("45"); arr.push_back("4"); store = arr; len = arr.size(); printLargest((1<<len)-1,""); printf("%s", res); return 0; }wouldn't a variation of radix sort work? By keeping in mind the most significant digit till comparisons end for each numbered place.
haha
import java.util.*; import java.io.*; public class intut{ public static void main(String []args) { String A[]={"43","44","12","324","90","9","88","89"}; String B; int i=0,j; //find the largest number formed by the numbers in the array Arrays.sort(A); while(i < A.length){ System.out.println(A[i]); ++i; } j = A.length; B = A[j-1]; System.out.println(B); i = A.length-1; while (i > 0){ String XY = (B+A[i-1]); String YX = (A[i-1]+B); String D[]={XY,YX}; Arrays.sort(D); B = D[1]; --i; } System.out.println(B); } }I got a simple solution jst use itoa conversion for all no.'s and store all the strings in an array and sort them in descending lexicographical order and jst print all the string from the starting of the array.
thats simple and effective
void sortWithSpecificComparision(int *arr,int n)
{
int i,j,temp,t1,t2,t3,t4,c1=0,c2=0,k;
for(i=0;i <= (n-1) ;i++)
{
for(j=i+1;j 0)
{
t1 = t1/10;
c1++;
}
while(t2 > 0)
{
t2 = t2/10;
c2++;
}
t3 = arr[i] * pow(10,c2) + arr[j];
t4 = arr[j] * pow(10,c1) + arr[i];
if( t3 < t4)
{
temp = arr[i];
arr[i]=arr[j];
arr[j] = temp;
}
}
}
}
The given solution is cool and simple solution with sorting!
Keep it up!
Hi,
You can find java code here:
import java.util.Arrays; import java.util.Comparator; /** * Given an array of numbers, arrange them in a way that yields the largest * value. For example, if the given numbers are {54, 546, 548, 60}, the * arrangement 6054854654 gives the largest value. * * @author GAPIITD * */ public class ArrangeGivenNumbersToFormTheBiggestNumber { /** * @param args */ public static void main(String[] args) { Integer nos[] = new Integer[]{54,546,60,548}; for (int i : nos) { System.out.println(i); } Arrays.sort(nos, new comp()); System.out.println("sorted array: "); for (int i : nos) { System.out.println(i); } System.out.println("BiggestNumber: "); for (int i : nos) { System.out.print(i); } } } class comp implements Comparator<Object> { @Override public int compare(Object arg1, Object arg2) { Integer i1 = (Integer)arg1; Integer i2 = (Integer)arg2; String st1 = (String.valueOf(i1).concat(String.valueOf(i2))); String st2 = (String.valueOf(i2).concat(String.valueOf(i1))); System.out.println("st1= " + st1 + " st2 = " + st2); if (Integer.parseInt(st1) > Integer.parseInt(st2)) return -1; return 1; } }if condition is not required.
one more way to do is
public int compare(Object arg1, Object arg2) { Integer i1 = (Integer) arg1; Integer i2 = (Integer) arg2; return (i1.intValue()+"").compareTo(i2.intValue()+""); }Wouldn't it be sufficient to get each digit in the array and sort them in ascending sort and concatenate them to get the greatest number.
Sorry for the last comment, it won't work.
You only need to get the greatest first digits first and when there are two or more, use compare function to find out which has to be place before the other
the compare function could be modified as
int myCompare(string X, string Y)
{
return (X[0]-'0') > (Y[0]-'0') ? 1 : 0;
}
because ultimately wt ur code does is just comparing the first digit of the two numbers nd returns the one which has the greatest first digit.....
sorry...the above case fails when both the numbers have the same first digit....
sorry the above code wasn't correctly uploaded....
plz let me know if the code below falters somewhere
#include<map> #include<iostream> using namespace std; int main(){ int n,i; cin>>n; map<string,int> mp; string num; for(i=0;i<n;i++){ cin>>num; mp[num]++; } map<string,int> ::reverse_iterator it; for(it=mp.rbegin();it!=mp.rend();it++){ if(it->second!=1){ while(it->second--) cout<<it->first; } else cout<<it->first; } return 0; }Very good problem and a simple neat solution
#include