Minimum sum | Practice | GeeksforGeeks

Given an array Arr of size N such that each element is from the range 0 to 9. Find the minimum possible sum of two numbers formed using the elements of the array. All digits in the given array must be used to form the two numbers.

Example 1:
Input:
N = 6
Arr[] = {6, 8, 4, 5, 2, 3}
Output: 604
Explanation: The minimum sum is formed 
by numbers 358 and 246.

Example 2:
Input:
N = 5
Arr[] = {5, 3, 0, 7, 4}
Output: 82
Explanation: The minimum sum is 
formed by numbers 35 and 047.

Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes arr[] and n as input parameters and returns the minimum possible sum. As the number can be large, return string presentation of the number without leading zeroes.
 
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 107
0 ≤ Arri ≤ 9
0
suprithsk20014 days ago
   #easy python sol 
arr.sort()                   s1=''       s2=''       for i in range(n):           if i%2==0:              s1+=str(arr[i])           else:              s2+=str(arr[i])             if(len(s1)==0):           return int(s2)       elif(len(s2)==0):           return int(s1)              return int(s1)+int(s2)
0
joyrockok1 week ago
class Solution {   String solve(int[] arr, int n) { StringBuilder sb = new StringBuilder();  Arrays.sort(arr);  String ansStr=""; int a=0, b=0; int olim=0, remain=0;  for(int i=n-1; i>=0; i=i-2) {    if(i%2 == 0) {   a = arr[i];   if(i-1>=0)     b = arr[i-1];   else     b=0;  } else {   a = arr[i];   b = arr[i-1];  }    int sum=a+b+olim;     if(sum>=10) {   olim=1;   remain = (sum)%10;  } else {   olim=0;   remain = sum;  }    sb.append(remain); }        if(olim==1) sb.append(1);  sb.reverse();  int i=0; int c = sb.charAt(i) - '0'; while(sb.charAt(i)-'0' == 0) {  i++; }  ansStr = sb.substring(i,sb.length());  return ansStr;
   }}
0
joyrockok
This comment was deleted.
0
sudeepgarg6712 weeks ago
def solve(self, arr, n):       arr1 = sorted(arr)       sum1 = 0       sum2 = 0       for i in range(0,n,2):           sum1 = sum1*10 + arr1[i]       for i in range(1,n,2):           sum2 =sum2*10 + arr1[i]       return sum1 + sum2
0
hydracody454 weeks ago
Upvote if help: cpp EASY OOFN 
Her my solution:
string solve(int arr[], int n) {
        // code here
        sort(arr,arr+n);
        int i=0;
        string a,b;
        a="";b="";
        while(i<n){
            if(arr[i]==0){
                i++;
                continue;
            }
            a=(char)(arr[i]+'0')+a;
            i++;
            // cout<<(char)(arr[i]+'0');
            if(i<n){
                b=(char)(arr[i]+'0')+b;
                i++;
            }

        }
        // cout<<"a="<<a<<"b= "<<b<<endl;
        int c=0,j=0;i=0;
        string s="";
        while(i<a.length() && j<b.length()){
            int x=(int)(a[i]-'0')+(int)(b[i]-'0')+c;
            s=(char)((x%10)+'0')+s;
            c=x/10;
            i++;j++;
            // cout<<"s="<<s<<endl;
        }

        while(i<a.length()){
            int x=(int)(a[i]-'0')+c;
            s=(char)((x%10)+'0')+s;
            c=x/10;
            i++;
        }
        while(j<b.length()){
            int x=(int)(b[j]-'0')+c;
            s=(char)((x%10)+'0')+s;
            c=x/10;
            j++;
        }
        if(c!=0){
            s=(char)(c+'0')+s;
        }
        
        return s;
    }
0
jom6655441 month ago
def solve(self, arr, n):
        arr_sort = sorted(arr)
        if n == 1:
            return arr[0]
        return int(''.join(map(str,arr_sort[0:n:2]))) + int(''.join(map(str,arr_sort[1:n:2])))
0
srivasaurabh791 month ago
class Solution:
   def solve(self, arr, n):
       # code here
       if(n == 1):
           return str(arr[0])
       arr.sort()
       
       i = 0
       s1 = "" 
       s2 = ""
       
       while(i < n):
           s1 += str(arr[i])
           i += 2
       i = 1 
       
       while(i < n):
           s2 += str(arr[i])
           i += 2
       
       ans = int(s1) + int(s2)
       
       return str(ans)
0
parthbabbar0011 month ago
C++ Solution
// { Driver Code Starts
//Initial template for C++

#include <bits/stdc++.h>
using namespace std;

 // } Driver Code Ends
//User function template for C++

class Solution{   
public:
    string solve(int arr[], int n) {
        // code here
        priority_queue<int,vector<int>,greater<int> > pq;
        for(int i=0;i<n;i++){
            if(arr[i]!=0){
                pq.push(arr[i]);
            }
        }
        
        string f;
        string s;
        while(!pq.empty()){
            int x = pq.top();
            pq.pop();
            if(!pq.empty()){
                int y = pq.top();
                pq.pop();
                f += to_string(x);
                s += to_string(y);
            }
            else{
                f += to_string(x);
            }
        }
        
        string sum="";
         int c=0;
         int i=f.size()-1;
         int j=s.size()-1;
        while(i>=0 and j>=0){
             int x=f[i]-'0';
             int y=s[j]-'0';
             int l=(x+y+c)%10;
             c=(x+y+c)/10;
             sum+=to_string(l);
             i--;
             j--;
         }
         while(i>=0){
             int x=f[i]-'0';
             int l=(x+c)%10;
             c=(x+c)/10;
             sum+=to_string(l);
             i--;
         }
         while(j>=0){
             int y=s[j]-'0';
             int l=(y+c)%10;
             c=(y+c)/10;
             sum+=to_string(l);
             j--;
         }
         if(c!=0){
             sum+=to_string(c);
         }
         
         reverse(sum.begin(),sum.end());
         return sum;    
    }
};

// { Driver Code Starts.
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int arr[n];
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        Solution ob;
        auto ans = ob.solve(arr, n);
        cout << ans << "\n";
    }
    return 0;
}  // } Driver Code Ends
+2
patildhiren441 month ago
Java
String solve(int[] arr, int n) {
        // code here
        StringBuilder sb = new StringBuilder();
        PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
        
        for(int i=0; i<n; i++){
            pq.add(arr[i]);
        }   
        int car = 0;
        while(!pq.isEmpty()){
            int a = pq.remove();
            int b = 0;            
            if(!pq.isEmpty()){
                b = pq.remove();
            }            
            int sum = a + b + car;            
            if(sum>9){
                car = 1;
            }else{
                car = 0;
            }            
            sum = sum %10;
            sb.append(sum);
        }
        
        if(car == 1){
            sb.append(1);
        }        
        sb.reverse();        
        while(sb.charAt(0) == '0'){
            sb.deleteCharAt(0);
        }        
        return sb.toString();
    }
+1
sanketbhagat2 months ago
SIMPLE JAVA SOLUTION
class Solution {
    String solve(int[] arr, int n) {
        // code here
        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);
        for(int i: arr) pq.offer(i);
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        while(!pq.isEmpty()){
            int a = pq.poll();
            int b = pq.isEmpty()?0:pq.poll();
            int sum = a+b+carry;
            carry = sum>9?1:0;
            sum %= 10;
            sb.append(sum);
        }
        if(carry==1) sb.append(1);
        sb.reverse();
        while(sb.charAt(0)=='0') sb.deleteCharAt(0);
        return sb.toString();
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.