Sum of two large numbers | Practice | GeeksforGeeks

Given two strings denoting non-negative numbers X and Y. Calculate the sum of X and Y. 

Example 1:
Input:
X = "25", Y = "23"
Output:
48
Explanation:
The sum of 25 and 23 is 48.
Example 2:
Input:
X = "2500", Y = "23"
Output:
2523
Explanation:
The sum of 2500 and 23 is 2523.

Your Task:
Your task is to complete the function findSum() which takes two strings as inputs and returns the string without leading zeros. You do not need to take any input or print anything.

Expected Time Complexity: O(|X| + |Y|)
Expected Auxiliary Space: O(1)

Constraints:
1 <= |X|, |Y| <= 105
0
mahesh_phutane1 week ago
Simple java solution
class Solution {
    String findSum(String X, String Y) {
        StringBuilder sb = new StringBuilder("");
        int xlen = X.length();
        int ylen = Y.length();
        int carry = 0;
        int i = xlen-1;
        int j = ylen-1;
        
        while(i>-1 || j>-1 || carry!=0){
            int xsum = i>-1?X.charAt(i--)-'0':0;
            int ysum = j>-1?Y.charAt(j--)-'0':0;
            int tsum = xsum+ysum+carry;
            sb.append(Integer.toString(tsum%10));
            carry = tsum/10;
        }
        
        j = sb.length()-1;
        
        while(sb.charAt(j)=='0' && j>0){
            sb.deleteCharAt(j--);
        }
        
        return sb.reverse().toString();
    }
}
-1
sunilkumarhotta12 weeks ago
        return (new java.math.BigInteger(X).add(new java.math.BigInteger(Y))).toString(); 
-1
aakasshuit3 weeks ago
//Java Solution

        java.math.BigInteger b1 = new java.math.BigInteger(X);
        java.math.BigInteger b2 = new java.math.BigInteger(Y);
        
        java.math.BigInteger ans = b1.add(b2);
        
        return ans.toString();
+1
prishita1 month ago
JAVA Solution:
Using BigInteger
 
class Solution {
    String findSum(String X, String Y) {
        // code here
        java.math.BigInteger n1= new java.math.BigInteger(X);
        java.math.BigInteger n2= new java.math.BigInteger(Y);
        java.math.BigInteger sum;
        sum=n1.add(n2);
        return sum.toString();
    }
}
0
ayazmrz981 month ago
def findSum(self, X, Y):
 return int(X)+int(Y)
+1
sharma_nitin_261 month ago
0
anantkumawat101102 months ago
C++ Solution.
string findSum(string X, string Y) {
    string str;
    int i = X.size() - 1, j = Y.size() - 1, carry = 0, first,
        second, sum;
        
    while(i >= 0 || j >= 0 || carry){
        if(i >= 0) first = X[i--] - '0';
        else first = 0;
          
        if(j >= 0) second = Y[j--] - '0';
        else second = 0;
            
        sum = first + second + carry;
        str.insert(0, to_string(sum % 10));
        carry = sum / 10;
    }
    i = 0;
    while(str.size() > 1 && str[i] == '0'){ 
        str.erase(0, 1);
    }
        
    return str;
}
0
mnkbutola2 months ago
 string findSum(string X, string Y) {       // Your code goes here       int i=X.size()-1,j=Y.size()-1;       int cary=0;       string ans="";       while(i>=0 or j>=0 or cary){           int sum=0;           if(i>=0){               sum+=X[i]-'0';               i--;           }           if(j>=0){               sum+=Y[j]-'0';               j--;           }           sum+=cary;           cary=sum/10;           ans=to_string(sum%10)+ans;       }           for( i=0;i<ans.size();i++){           if(ans[i]=='0') continue;           else break;       }       if(i==ans.size()) return "0";       else return ans.substr(i);   }
+1
sachinupreti1902 months ago
C++ EASY CODE.
PLEASE UPVOTE, IF it helps you .
 
// Here is your code.
 
class Solution { public:   string findSum(string X, string Y) {          string ptr="";     int m,n,i,c=0;     m=X.size();     n=Y.size();     for(i=0;i<m;i++)     {         if(X[i]=='0' && c<m-1)         {             c++;          }         else         break;     }     X=X.erase(0,c);                    c=0;          for(i=0;i<n;i++)     {         if(Y[i]=='0' && c<n-1)         {             c++;          }         else         break;     }     Y=Y.erase(0,c);     if(X.size()<Y.size())     {         for(i=1;i<=Y.size()-X.size();i++)         {             ptr+="0";                }         X=ptr+X;              }     //return ptr;     else if(X.size()==Y.size())       {              }     else     {         for(i=1;i<=X.size()-Y.size();i++)         {             ptr+="0";         }         Y=ptr+Y;     }     reverse(X.begin(),X.end());     reverse(Y.begin(),Y.end());     int carry=0,a,b,l,f;     string final="",qtr;     for(i=0;i<X.size();i++)     {          a=X[i]-48;                                                                      b=Y[i]-48;                                                                                                           l=a+b+carry;                                 f=l%10;         qtr=to_string(f);         final=final+qtr;         carry=l/10;     }     if(carry>0)     final+=to_string(carry);     reverse(final.begin(),final.end());      return final;            }
0
ajeetkumarece222 months ago
int charToint(char ch){   return ch-'0';}char intTochar(int num){   return num+'0';}
string helper(string res){    int i=0;    while(i<res.length() && res[i]=='0'){        i++;    }    if(i==res.length()){        return "0";    }    return res.substr(i); }   string findSum(string X, string Y) {       // Your code goes here       if(X.length()>Y.length()) swap(X,Y);        reverse(X.begin(),X.end());         reverse(Y.begin(),Y.end());       string result="";       int carry=0;       for(int i=0;i<X.length();i++){           int d1=charToint(X[i]);           int d2=charToint(Y[i]);           int sum=d1+d2+carry;           int op=sum%10;           carry=sum/10;           result.push_back(intTochar(op));       }       for(int i=X.length();i<Y.length();i++){           int d1=charToint(Y[i]);           int sum=d1+carry;           int op=sum%10;           carry=sum/10;           result.push_back(intTochar(op));       }       if(carry) result.push_back('1');       reverse(result.begin(),result.end());       string r=helper(result);       return r;
}
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.