Given 4 integers A, B, Z1, and Z2. The task is to compare A*10Z1 and B*10Z2.
Examples:
Input: A = 19, Z1 = 2, B = 20, Z2 = 1
Output: A > B
Explanation:
A can be written as 1900
B can be written as 200
So, A is greater than B.Input:, A = 199, Z1 =10, B = 96, Z2 = 1000
Output: A < B
Explanation:
A can be written as 19900000....
B can be written as 9600000......
So, A is smaller than B
Naive Approach : Multiply A with Z1 zeroes and B with Z2 zeroes and compare both But large number cannot be store in long long integer more than 18 digits.
Time Complexity: O(1)
Auxiliary Space: O(1)
Efficient Approach: The idea is to compare the total number of digits in A and B because the largest digit number is maximum than the other.
- Take two variables and adigits and bdigits and initialize to zero.
- Initialize the variables tempA and tempB as A and B and traverse in the while loop and store the number of digits in A and B.
- Compare the values of adigits+z1 and bdigits+z2. If their values are equal, then perform the following tasks:
- If adigits is greater than bdigits, then initialize the variables addZeros as adigits-bdigits and b as 10addZeros and vice-versa if bdigits is greater than adigits.
- Now, compare the values of a and b, perform the results.
Below is the implementation of the above approach.
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to compare 2 numbers A and B
string CompareNumbers(int a, int b,
int z1, int z2)
{
// Calculate number of digits
// in both the numbers
int Adigits = 0, Bdigits = 0;
int tempA = a, tempB = b;
while (tempA != 0) {
Adigits++;
tempA /= 10;
}
while (tempB != 0) {
Bdigits++;
tempB /= 10;
}
// Now compare both the digits with
// adding zeroes
if (Adigits + z1 > Bdigits + z2) {
return ">";
}
else if (Adigits + z1 < Bdigits + z2) {
return "<";
}
// If both condition are not true means
// they have equal digits So now add zeroes
// in smaller digit number to make equal
// digits number as larger
if (Adigits > Bdigits) {
int addzeroes = Adigits - Bdigits;
b *= pow(10, addzeroes);
}
else {
int addzeroes = Bdigits - Adigits;
a *= pow(10, addzeroes);
}
if (a == b) {
return "=";
}
else if (a > b) {
return ">";
}
else {
return "<";
}
}
// Driver Code
int main()
{
int a = 20, z1 = 2;
int b = 200, z2 = 1;
string ans = CompareNumbers(a, b, z1, z2);
cout << "A " << ans << " B";
return 0;
}
// Java program for the above approach
class GFG {
// Function to compare 2 numbers A and B
static String CompareNumbers(int a, int b,
int z1, int z2) {
// Calculate number of digits
// in both the numbers
int Adigits = 0, Bdigits = 0;
int tempA = a, tempB = b;
while (tempA != 0) {
Adigits++;
tempA /= 10;
}
while (tempB != 0) {
Bdigits++;
tempB /= 10;
}
// Now compare both the digits with
// adding zeroes
if (Adigits + z1 > Bdigits + z2) {
return ">";
} else if (Adigits + z1 < Bdigits + z2) {
return "<";
}
// If both condition are not true means
// they have equal digits So now add zeroes
// in smaller digit number to make equal
// digits number as larger
if (Adigits > Bdigits) {
int addzeroes = Adigits - Bdigits;
b *= (int) Math.pow(10, addzeroes);
} else {
int addzeroes = Bdigits - Adigits;
a *= (int) Math.pow(10, addzeroes);
}
if (a == b) {
return "=";
} else if (a > b) {
return ">";
} else {
return "<";
}
}
// Driver Code
public static void main(String args[]) {
int a = 20, z1 = 2;
int b = 200, z2 = 1;
String ans = CompareNumbers(a, b, z1, z2);
System.out.println("A " + ans + " B");
}
}
// This code is contributed by gfgking
# Python code for the above approach
# Function to compare 2 numbers A and B
def CompareNumbers(a, b, z1, z2):
# Calculate number of digits
# in both the numbers
Adigits = 0
Bdigits = 0
tempA = a
tempB = b
while (tempA != 0):
Adigits += 1
tempA = tempA // 10
while (tempB != 0):
Bdigits += 1
tempB = tempB // 10
# Now compare both the digits with
# adding zeroes
if (Adigits + z1 > Bdigits + z2):
return ">";
elif (Adigits + z1 < Bdigits + z2):
return "<";
# If both condition are not true means
# they have equal digits So now add zeroes
# in smaller digit number to make equal
# digits number as larger
if (Adigits > Bdigits):
addzeroes = Adigits - Bdigits;
b *= (10 ** addzeroes)
else:
addzeroes = Bdigits - Adigits;
a *= (10 ** addzeroes)
if (a == b):
return "=";
elif (a > b):
return ">";
else:
return "<";
# Driver Code
a = 20
z1 = 2;
b = 200
z2 = 1;
ans = CompareNumbers(a, b, z1, z2);
print("A " + ans + " B");
# This code is contributed by gfgking
// C# program for the above approach
using System;
class GFG
{
// Function to compare 2 numbers A and B
static string CompareNumbers(int a, int b,
int z1, int z2)
{
// Calculate number of digits
// in both the numbers
int Adigits = 0, Bdigits = 0;
int tempA = a, tempB = b;
while (tempA != 0) {
Adigits++;
tempA /= 10;
}
while (tempB != 0) {
Bdigits++;
tempB /= 10;
}
// Now compare both the digits with
// adding zeroes
if (Adigits + z1 > Bdigits + z2) {
return ">";
}
else if (Adigits + z1 < Bdigits + z2) {
return "<";
}
// If both condition are not true means
// they have equal digits So now add zeroes
// in smaller digit number to make equal
// digits number as larger
if (Adigits > Bdigits) {
int addzeroes = Adigits - Bdigits;
b *= (int)Math.Pow(10, addzeroes);
}
else {
int addzeroes = Bdigits - Adigits;
a *= (int)Math.Pow(10, addzeroes);
}
if (a == b) {
return "=";
}
else if (a > b) {
return ">";
}
else {
return "<";
}
}
// Driver Code
public static void Main()
{
int a = 20, z1 = 2;
int b = 200, z2 = 1;
string ans = CompareNumbers(a, b, z1, z2);
Console.Write("A " + ans + " B");
}
}
// This code is contributed by Samim Hossain Mondal.
<script>
// JavaScript code for the above approach
// Function to compare 2 numbers A and B
function CompareNumbers(a, b, z1, z2)
{
// Calculate number of digits
// in both the numbers
let Adigits = 0, Bdigits = 0;
let tempA = a, tempB = b;
while (tempA != 0)
{
Adigits++;
tempA = Math.floor(tempA / 10);
}
while (tempB != 0)
{
Bdigits++;
tempB = Math.floor(tempB / 10);
}
// Now compare both the digits with
// adding zeroes
if (Adigits + z1 > Bdigits + z2)
{
return ">";
}
else if (Adigits + z1 < Bdigits + z2)
{
return "<";
}
// If both condition are not true means
// they have equal digits So now add zeroes
// in smaller digit number to make equal
// digits number as larger
if (Adigits > Bdigits)
{
let addzeroes = Adigits - Bdigits;
b *= Math.pow(10, addzeroes);
}
else
{
let addzeroes = Bdigits - Adigits;
a *= Math.pow(10, addzeroes);
}
if (a == b)
{
return "=";
}
else if (a > b)
{
return ">";
}
else
{
return "<";
}
}
// Driver Code
let a = 20, z1 = 2;
let b = 200, z2 = 1;
let ans = CompareNumbers(a, b, z1, z2);
document.write("A " + ans + " B");
// This code is contributed by Potta Lokesh
</script>
Output
A = B
Time Complexity: O(1)
Auxiliary Space: O(1)