TechoSagar: Minimum sum

Search

Google Alert - jobs

Minimum sum

Given an array of digits (values are from 0 to 9), find the minimumpossible 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
Your Code
#include<iostream>

using namespace std;
#include<math.h>
int main()
{
int t;
cin>>t;
while(t--)
{
int a[30],n;
cin>>n;
int i;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int j;
for(i=0; i<n-1; i++)
{    int c=i;
for(j=i+1; j<n; j++)
if(a[c]>a[j])
{
c=j;
}
}
if(c!=i)
{
int temp=a[i];
a[i]=a[c];
a[c]=temp;
}
long long int num1=0,num2=0;
int k=0;
for(i=n-1; i>=0; i--)
{    // cout<<a[i]<<endl;
num1=num1+a[i]*pow(10,k);
i--;
if(i>=0)
{
num2=num2+a[i]*pow(10,k);
     
//cout<<a[i]<<endl;
     }
k++;
//cout<<num1<<" "<<num2;
}
cout<<num1+num2<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook