Largest Number formed from an Array
PROBLEM :
Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.
The result is going to be very large, hence return the result in the form of a string.
Input:
The first line of input consists number of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.
Output:
In each separate line print the largest number formed by arranging the elements of the array in the form of a string.
Constraints:
1 = T = 70
1 = N = 100
0 = A[i] = 1000
Example:
Input:
2
5
3 30 34 5 9
4
54 546 548 60
Output:
9534330
6054854654
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<algorithm>
#include<string.h>
#include <stdlib.h>
#define max 100
string Largest_Number(int [],int ) ;
int compare(int ,int ) ;
int append(int ,int ) ;
int main()
{
int arr[max] ;
int i,t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cout<<Largest_Number(arr,no)<<endl ;
}
return 0;
}
string Largest_Number(int arr[],int no)
{
int i ;
string str="" ;
sort(arr,arr+no,compare) ;
for(i=0;i<no;i++)
str=str+to_string(arr[i]) ;
return str ;
}
int compare(int a,int b)
{
return append(a,b)>append(b,a) ;
}
int append(int a,int b)
{
int temp[10] ;
int i,r ;
i=0 ;
while(b)
{
r=b%10 ;
temp[i++]=r ;
b=b/10 ;
}
for(r=i-1;r>=0;r--)
a=a*10+temp[r] ;
if(i==0)
a=a*10 ;
return a ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (better solution)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int compare(string s1,string s2)
{
return s1+s2>s2+s1 ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
vector<string> str ;
int n ;
for(int i=0;i<no;i++)
{
cin>>n ;
str.push_back(to_string(n)) ;
}
sort(str.begin(),str.end(),compare) ;
for(int i=0;i<no;i++)
cout<<str[i] ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Given a list of non negative integers, arrange them in such a manner that they form the largest number possible.
The result is going to be very large, hence return the result in the form of a string.
Input:
The first line of input consists number of the test cases. The description of T test cases is as follows:
The first line of each test case contains the size of the array, and the second line has the elements of the array.
Output:
In each separate line print the largest number formed by arranging the elements of the array in the form of a string.
Constraints:
1 = T = 70
1 = N = 100
0 = A[i] = 1000
Example:
Input:
2
5
3 30 34 5 9
4
54 546 548 60
Output:
9534330
6054854654
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<algorithm>
#include<string.h>
#include <stdlib.h>
#define max 100
string Largest_Number(int [],int ) ;
int compare(int ,int ) ;
int append(int ,int ) ;
int main()
{
int arr[max] ;
int i,t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cout<<Largest_Number(arr,no)<<endl ;
}
return 0;
}
string Largest_Number(int arr[],int no)
{
int i ;
string str="" ;
sort(arr,arr+no,compare) ;
for(i=0;i<no;i++)
str=str+to_string(arr[i]) ;
return str ;
}
int compare(int a,int b)
{
return append(a,b)>append(b,a) ;
}
int append(int a,int b)
{
int temp[10] ;
int i,r ;
i=0 ;
while(b)
{
r=b%10 ;
temp[i++]=r ;
b=b/10 ;
}
for(r=i-1;r>=0;r--)
a=a*10+temp[r] ;
if(i==0)
a=a*10 ;
return a ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (better solution)
--------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int compare(string s1,string s2)
{
return s1+s2>s2+s1 ;
}
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
vector<string> str ;
int n ;
for(int i=0;i<no;i++)
{
cin>>n ;
str.push_back(to_string(n)) ;
}
sort(str.begin(),str.end(),compare) ;
for(int i=0;i<no;i++)
cout<<str[i] ;
cout<<endl ;
}
return 0;
}
---------------------------------------------------------------------------------
Comments
Post a Comment