Compare given two powers of 10
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++
#include <bits/stdc++.h>
using namespace std;
string CompareNumbers( int a, int b,
int z1, int z2)
{
int Adigits = 0, Bdigits = 0;
int tempA = a, tempB = b;
while (tempA != 0) {
Adigits++;
tempA /= 10;
}
while (tempB != 0) {
Bdigits++;
tempB /= 10;
}
if (Adigits + z1 > Bdigits + z2) {
return ">" ;
}
else if (Adigits + z1 < Bdigits + z2) {
return "<" ;
}
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 "<" ;
}
}
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
class GFG {
static String CompareNumbers( int a, int b,
int z1, int z2) {
int Adigits = 0 , Bdigits = 0 ;
int tempA = a, tempB = b;
while (tempA != 0 ) {
Adigits++;
tempA /= 10 ;
}
while (tempB != 0 ) {
Bdigits++;
tempB /= 10 ;
}
if (Adigits + z1 > Bdigits + z2) {
return ">" ;
} else if (Adigits + z1 < Bdigits + z2) {
return "<" ;
}
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 "<" ;
}
}
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" );
}
}
|
Python3
def CompareNumbers(a, b, z1, z2):
Adigits = 0
Bdigits = 0
tempA = a
tempB = b
while (tempA ! = 0 ):
Adigits + = 1
tempA = tempA / / 10
while (tempB ! = 0 ):
Bdigits + = 1
tempB = tempB / / 10
if (Adigits + z1 > Bdigits + z2):
return ">" ;
elif (Adigits + z1 < Bdigits + z2):
return "<" ;
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 "<" ;
a = 20
z1 = 2 ;
b = 200
z2 = 1 ;
ans = CompareNumbers(a, b, z1, z2);
print ( "A " + ans + " B" );
|
C#
using System;
class GFG
{
static string CompareNumbers( int a, int b,
int z1, int z2)
{
int Adigits = 0, Bdigits = 0;
int tempA = a, tempB = b;
while (tempA != 0) {
Adigits++;
tempA /= 10;
}
while (tempB != 0) {
Bdigits++;
tempB /= 10;
}
if (Adigits + z1 > Bdigits + z2) {
return ">" ;
}
else if (Adigits + z1 < Bdigits + z2) {
return "<" ;
}
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 "<" ;
}
}
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" );
}
}
|
Javascript
<script>
function CompareNumbers(a, b, z1, z2)
{
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);
}
if (Adigits + z1 > Bdigits + z2)
{
return ">" ;
}
else if (Adigits + z1 < Bdigits + z2)
{
return "<" ;
}
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 "<" ;
}
}
let a = 20, z1 = 2;
let b = 200, z2 = 1;
let ans = CompareNumbers(a, b, z1, z2);
document.write( "A " + ans + " B" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...