Subtraction of two large numbers using 10’s compliment

Given two strings str1 and str2 of given lengths N and M respectively, each representing a large number, the task is to subtract one from the other using 10’s complement.

Example:

Input: N = 10, str1 = “3434243434”, M = 14, str2 = “22324365765767”
Output: 22320931522333

Input: N = 20, str1 = “12345334233242431433”, M = 20, str2 = “12345334233242431432”
Output: 1

Approach: The basic idea is similar to Subtraction of two numbers using 2’s complement.



Subtraction of given strings can be written as
Str1 – Str2 = Str1 + (- Str2) = Str1 + (10’s complement of Str2)

Follow the steps below to solve the problem:

  • Compare the lengths of the two strings and store the smaller of the two in str2.
  • Calculate 10’s compliment of str2.
  • Now, add 10’s compliment of str2 to str1.
  • If any carry is generated, then drop the carry.
  • If no carry is generated, then the compliment of str1 is the final answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to calculate the
// subtraction of two large number
// using 10's complement
#include <bits/stdc++.h>
using namespace std;
  
// Function to return sum of two
// large numbers given as strings
string sumBig(string a, string b)
{
  
    // Compare their lengths
    if (a.length() > b.length())
        swap(a, b);
  
    // Stores the result
    string str = "";
  
    // Store the respective lengths
    int n1 = a.length(), n2 = b.length();
  
    int diff = n2 - n1;
  
    // Initialize carry
    int carry = 0;
  
    // Traverse from end of both strings
    for (int i = n1 - 1; i >= 0; i--) {
  
        // Compute sum of
        // current digits and carry
        int sum
            = ((a[i] - '0')
               + (b[i + diff] - '0') + carry);
  
        // Store the result
        str.push_back(sum % 10 + '0');
  
        // Update carry
        carry = sum / 10;
    }
  
    // Add remaining digits of str2[]
    for (int i = n2 - n1 - 1; i >= 0; i--) {
  
        int sum = ((b[i] - '0') + carry);
  
        str.push_back(sum % 10 + '0');
        carry = sum / 10;
    }
  
    // Add remaining carry
    if (carry)
        str.push_back(carry + '0');
  
    // Reverse resultant string
    reverse(str.begin(), str.end());
  
    return str;
}
  
// Function return 10's
// complement of given number
string compliment10(string v)
{
    // Stores the compliment
    string compliment = "";
  
    // Calculate 9's compliment
    for (int i = 0; i < v.size(); i++) {
  
        // Subtract every bit from 9
        compliment += '9' - v[i] + '0';
    }
  
    // Add 1 to 9's compliment
    // to find 10's compliment
    compliment = sumBig(compliment, "1");
    return compliment;
}
  
// Function returns subtraction
// of two given numbers as strings
string subtract(string a, string b)
{
  
    // If second string is larger
    if (a.length() < b.length())
        swap(a, b);
  
    // Calculate respective lengths
    int l1 = a.length(), l2 = b.length();
  
    // If lengths aren't equal
    int diffLen = l1 - l2;
  
    for (int i = 0; i < diffLen; i++) {
  
        // Insert 0's to the beginning
        // of b to make both the lengths equal
        b = "0" + b;
    }
  
    // Add (complement of B) and A
    string c = sumBig(a, compliment10(b));
  
    // If length of new string is greater
    // than length of first string,
    // than carry is generated
    if (c.length() > a.length()) {
        string::iterator it;
  
        // Erase first bit
        it = c.begin();
  
        c.erase(it);
  
        // Trim zeros at the begnning
        it = c.begin();
  
        while (*it == '0')
            c.erase(it);
  
        return c;
    }
  
    // If both lengths are equal
    else {
        return compliment10(c);
    }
}
  
// Driver Code
int main()
{
  
    string str1 = "12345334233242431433";
    string str2 = "12345334233242431432";
  
    cout << subtract(str1, str2) << endl;
  
    return 0;
}

chevron_right


Output:

1

Time Complexity: O(max(N, M))
Auxiliary Space: O(max(N, M))

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.