Program to Subtract two integers of given base
Last Updated :
02 Dec, 2023
Given three positive integers X, Y, and B, where X and Y are Base-B integers, the task is to find the value of X - Y such that X >= Y.
Examples:
Input: X = 1212, Y = 256, B = 8
Output: 0734
Explanation: The value of 1212 - 256 in base 8 is 734.
Input: X = 546, Y = 248, B = 9
Output: 287
Approach: The given problem can be solved by using basic mathematics subtraction. Follow the steps below to solve the given problem:
- Initialize two variables say power = 1, carry = 0, to keep track of current power and carry generated while subtracting respectively.
- Initialize a variable, say finalVal = 0, to store the resultant value of X - Y.
- Iterate a loop until X > 0 and perform the following steps:
- Store last digits from the current value of X and Y in two variables, say n1 = X % 10 and n2 = Y % 10 respectively.
- Remove last digits from X and Y by updating X = X / 10 and Y = Y / 10.
- Initialize temp = n1 - n2 + carry.
- If temp < 0, then add base B to N, that is N = N + B and set carry = -1, which will act as a borrow. Otherwise, set carry = 0.
- Add current temp * power to finalVal, that is finalVal = finalVal + temp * power and set power = power * 10.
- After completing the above steps, print the value of finalVal as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find X - Y in base B
int getDifference(int B, int X, int Y)
{
// To store final answer
int finalVal = 0;
// To store carry generated
int carry = 0;
// To keep track of power
int power = 1;
while (X > 0) {
// Store last digits of current
// value of X and Y in n1 and
// n2 respectively
int n1 = X % 10;
int n2 = Y % 10;
// Remove last digits from
// X and Y
X = X / 10;
Y = Y / 10;
int temp = n1 - n2 + carry;
if (temp < 0) {
// Carry = -1 will act
// as borrow
carry = -1;
temp += B;
}
else {
carry = 0;
}
// Add in final result
finalVal += temp * power;
power = power * 10;
}
// Return final result
return finalVal;
}
// Driver Code
int main()
{
int X = 1212;
int Y = 256;
int B = 8;
cout << (getDifference(B, X, Y));
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find X - Y in base B
public static int getDifference(
int B, int X, int Y)
{
// To store final answer
int finalVal = 0;
// To store carry generated
int carry = 0;
// To keep track of power
int power = 1;
while (X > 0) {
// Store last digits of current
// value of X and Y in n1 and
// n2 respectively
int n1 = X % 10;
int n2 = Y % 10;
// Remove last digits from
// X and Y
X = X / 10;
Y = Y / 10;
int temp = n1 - n2 + carry;
if (temp < 0) {
// Carry = -1 will act
// as borrow
carry = -1;
temp += B;
}
else {
carry = 0;
}
// Add in final result
finalVal += temp * power;
power = power * 10;
}
// Return final result
return finalVal;
}
// Driver Code
public static void main(String[] args)
{
int X = 1212;
int Y = 256;
int B = 8;
System.out.println(
getDifference(B, X, Y));
}
}
Python3
# Python3 program for the above approach
# Function to find X - Y in base B
def getDifference(B, X, Y) :
# To store final answer
finalVal = 0;
# To store carry generated
carry = 0;
# To keep track of power
power = 1;
while (X > 0) :
# Store last digits of current
# value of X and Y in n1 and
# n2 respectively
n1 = X % 10;
n2 = Y % 10;
# Remove last digits from
# X and Y
X = X // 10;
Y = Y // 10;
temp = n1 - n2 + carry;
if (temp < 0) :
# Carry = -1 will act
# as borrow
carry = -1;
temp += B;
else :
carry = 0;
# Add in final result
finalVal += temp * power;
power = power * 10;
# Return final result
return finalVal;
# Driver Code
if __name__ == "__main__" :
X = 1212;
Y = 256;
B = 8;
print(getDifference(B, X, Y));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find X - Y in base B
public static int getDifference(int B, int X, int Y)
{
// To store final answer
int finalVal = 0;
// To store carry generated
int carry = 0;
// To keep track of power
int power = 1;
while (X > 0) {
// Store last digits of current
// value of X and Y in n1 and
// n2 respectively
int n1 = X % 10;
int n2 = Y % 10;
// Remove last digits from
// X and Y
X = X / 10;
Y = Y / 10;
int temp = n1 - n2 + carry;
if (temp < 0) {
// Carry = -1 will act
// as borrow
carry = -1;
temp += B;
}
else {
carry = 0;
}
// Add in final result
finalVal += temp * power;
power = power * 10;
}
// Return final result
return finalVal;
}
// Driver Code
public static void Main(string[] args)
{
int X = 1212;
int Y = 256;
int B = 8;
Console.WriteLine(getDifference(B, X, Y));
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Javascript program for the above approach
// Function to find X - Y in base B
function getDifference(B, X, Y) {
// To store final answer
let finalVal = 0;
// To store carry generated
let carry = 0;
// To keep track of power
let power = 1;
while (X > 0) {
// Store last digits of current
// value of X and Y in n1 and
// n2 respectively
let n1 = X % 10;
let n2 = Y % 10;
// Remove last digits from
// X and Y
X = Math.floor(X / 10);
Y = Math.floor(Y / 10);
let temp = n1 - n2 + carry;
if (temp < 0) {
// Carry = -1 will act
// as borrow
carry = -1;
temp += B;
} else {
carry = 0;
}
// Add in final result
finalVal += temp * power;
power = power * 10;
}
// Return final result
return finalVal;
}
// Driver Code
let X = 1212;
let Y = 256;
let B = 8;
document.write(getDifference(B, X, Y));
// This code is contributed by gfgking
</script>
Time Complexity: O(log10N)
Auxiliary Space: O(1)
However, we can optimize the solution by performing the following steps:
- First, convert the given numbers X and Y into decimal form.
- Then, find the difference between X and Y.
- Finally, convert the decimal difference back into the given base.
Here's the implementation of the above approach:
C++
//C++ progarm for above approach
#include <iostream>
#include <cmath>
using namespace std;
// Function to convert a number in given base
// to decimal form
int toDecimal(int num, int base) {
int res = 0, power = 1;
while (num > 0) {
int digit = num % 10;
res += digit * power;
power *= base;
num /= 10;
}
return res;
}
// Function to convert a decimal number
// to given base form
int toBase(int num, int base) {
int res = 0, power = 1;
while (num > 0) {
int digit = num % base;
res += digit * power;
power *= 10;
num /= base;
}
return res;
}
// Function to find X - Y in base B
int getDifference(int B, int X, int Y)
{
// Convert X and Y to decimal form
int X_dec = toDecimal(X, B);
int Y_dec = toDecimal(Y, B);
// Find the difference in decimal form
int diff_dec = X_dec - Y_dec;
// Convert the difference to base B form
int diff = toBase(diff_dec, B);
// Return the difference
return diff;
}
// Driver Code
int main()
{
int X = 1212;
int Y = 256;
int B = 8;
cout << getDifference(B, X, Y);
return 0;
}
Java
//Java code for above approach
import java.lang.Math;
public class Main {
// Function to convert a number in given base
// to decimal form
static int toDecimal(int num, int base) {
int res = 0, power = 1;
while (num > 0) {
int digit = num % 10;
res += digit * power;
power *= base;
num /= 10;
}
return res;
}
// Function to convert a decimal number
// to given base form
static int toBase(int num, int base) {
int res = 0, power = 1;
while (num > 0) {
int digit = num % base;
res += digit * power;
power *= 10;
num /= base;
}
return res;
}
// Function to find X - Y in base B
static int getDifference(int B, int X, int Y) {
// Convert X and Y to decimal form
int X_dec = toDecimal(X, B);
int Y_dec = toDecimal(Y, B);
// Find the difference in decimal form
int diff_dec = X_dec - Y_dec;
// Convert the difference to base B form
int diff = toBase(diff_dec, B);
// Return the difference
return diff;
}
// Driver Code
public static void main(String[] args) {
int X = 1212;
int Y = 256;
int B = 8;
System.out.println(getDifference(B, X, Y));
}
}
Python3
# Python code
# Function to convert a number in the given base to decimal form
def to_decimal(num, base):
res, power = 0, 1
while num > 0:
digit = num % 10
res += digit * power
power *= base
num //= 10
return res
# Function to convert a decimal number to the given base form
def to_base(num, base):
res, power = 0, 1
while num > 0:
digit = num % base
res += digit * power
power *= 10
num //= base
return res
# Function to find X - Y in base B
def get_difference(B, X, Y):
# Convert X and Y to decimal form
X_dec = to_decimal(X, B)
Y_dec = to_decimal(Y, B)
# Find the difference in decimal form
diff_dec = X_dec - Y_dec
# Convert the difference to base B form
diff = to_base(diff_dec, B)
return diff
# Driver Code
if __name__ == "__main__":
X = 1212
Y = 256
B = 8
print(get_difference(B, X, Y))
C#
using System;
class Program
{
// Function to convert a number in given base
// to decimal form
static int ToDecimal(int num, int baseValue)
{
int res = 0, power = 1;
while (num > 0)
{
int digit = num % 10;
res += digit * power;
power *= baseValue;
num /= 10;
}
return res;
}
// Function to convert a decimal number
// to given base form
static int ToBase(int num, int baseValue)
{
int res = 0, power = 1;
while (num > 0)
{
int digit = num % baseValue;
res += digit * power;
power *= 10;
num /= baseValue;
}
return res;
}
// Function to find X - Y in base B
static int GetDifference(int B, int X, int Y)
{
// Convert X and Y to decimal form
int X_dec = ToDecimal(X, B);
int Y_dec = ToDecimal(Y, B);
// Find the difference in decimal form
int diff_dec = X_dec - Y_dec;
// Convert the difference to base B form
int diff = ToBase(diff_dec, B);
// Return the difference
return diff;
}
// Driver Code
static void Main()
{
int X = 1212;
int Y = 256;
int B = 8;
Console.WriteLine(GetDifference(B, X, Y));
}
}
JavaScript
// Function to convert a number in given base to decimal form
function toDecimal(num, base) {
let res = 0, power = 1;
while (num > 0) {
const digit = num % 10;
res += digit * power;
power *= base;
num = Math.floor(num / 10);
}
return res;
}
// Function to convert a decimal number to given base form
function toBase(num, base) {
let res = 0, power = 1;
while (num > 0) {
const digit = num % base;
res += digit * power;
power *= 10;
num = Math.floor(num / base);
}
return res;
}
// Function to find X - Y in base B
function getDifference(B, X, Y) {
// Convert X and Y to decimal form
const X_dec = toDecimal(X, B);
const Y_dec = toDecimal(Y, B);
// Find the difference in decimal form
const diff_dec = X_dec - Y_dec;
// Convert the difference to base B form
const diff = toBase(diff_dec, B);
// Return the difference
return diff;
}
// Driver Code
const X = 1212;
const Y = 256;
const B = 8;
console.log(getDifference(B, X, Y));
Output:
734
Time Complexity: O(log^2 N)
Auxiliary Space: O(log N)
Similar Reads
Program to add two integers of given base Given three integers X, Y, and B, where X and Y are Base-B integers. The task is to find the sum of integers X and Y. Examples: Input: X = 123, Y = 234, B = 6 Output: 401 Explanation: Sum of two integers in base 6 - 1 1 1 2 3 + 2 3 4 ------------- 4 0 1 Input: X = 546, Y = 248 B = 9 Output: 805 Expl
9 min read
Program to Division two integers of given base Given three positive integers Base B, Dividend, and Divisor, where Dividend and Divisor are Base-B integers, the task is to find dividend/divisor. Examples: Input: Dividend = 513, Divisor = 7, B = 8Output: 57Explanation: The value of 513/7 in base 8 is 57. Input: Dividend = 400, Divisor = 20 B = 8Ou
7 min read
Program to find the last digit of X in base Y Given a positive integer X and Y, the task is to find the last digit of X in the given base Y. Examples: Input: X = 10, Y = 7 Output: 3 10 is 13 in base 9 with last digit 3 Input: X = 55, Y = 3 Output: 1 55 is 3 in base 601 with last digit 1 Approach: When we try to convert X into the base YWe repea
3 min read
Javascript Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no
5 min read
Write a program to add two numbers in base 14 Asked by Anshya. Base 14: Decimal numbersBase 14 numbers0011223344556677889910A11B12C13D14D1 Below are the different ways to add base 14 numbers.Method 1 Thanks to Raj for suggesting this method. 1. Convert both i/p base 14 numbers to base 10. 2. Add numbers. 3. Convert the result back to base 14. M
15 min read