Minimum sum
PROBLEM :
Given an array of digits (values are from 0 to 9), find the minimum possible sum of two numbers formed from digits of the array. All digits of given array must be used to form the two numbers.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the minimum possible sum of two numbers formed from digits of the array.
Constraints:
1 = T = 100
1 = N = 30
1 = A[i] = 9
Example:
Input
1
5
5 3 0 7 4
Output
82
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Minimum_sum(int [],int ) ;
void bubble(int [],int ) ;
int main()
{
int t,no,arr[30],i ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
ans=Minimum_sum(arr,no) ;
cout<<ans<<endl ;
}
return 0;
}
long long Minimum_sum(int arr[],int no)
{
bubble(arr,no) ;
long long a,b ;
int i ;
a=0 ;
b=0 ;
for(i=0;i<no;i++)
{
if(i%2==0)
a=a*10+arr[i] ;
else
b=b*10+arr[i] ;
}
return a+b ;
}
void bubble(int arr[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Given an array of digits (values are from 0 to 9), find the minimum possible sum of two numbers formed from digits of the array. All digits of given array must be used to form the two numbers.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines. The first line of each test case consists of an integer N, where N is the size of array.
The second line of each test case contains N space separated integers denoting array elements.
Output:
Corresponding to each test case, in a new line, print the minimum possible sum of two numbers formed from digits of the array.
Constraints:
1 = T = 100
1 = N = 30
1 = A[i] = 9
Example:
Input
1
5
5 3 0 7 4
Output
82
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
long long Minimum_sum(int [],int ) ;
void bubble(int [],int ) ;
int main()
{
int t,no,arr[30],i ;
long long ans ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
ans=Minimum_sum(arr,no) ;
cout<<ans<<endl ;
}
return 0;
}
long long Minimum_sum(int arr[],int no)
{
bubble(arr,no) ;
long long a,b ;
int i ;
a=0 ;
b=0 ;
for(i=0;i<no;i++)
{
if(i%2==0)
a=a*10+arr[i] ;
else
b=b*10+arr[i] ;
}
return a+b ;
}
void bubble(int arr[],int no)
{
int i,j,temp ;
for(i=0;i<no-1;i++)
{
for(j=0;j<no-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j] ;
arr[j]=arr[j+1] ;
arr[j+1]=temp ;
}
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment