Increase A by atmost B times so that zeroes at the end of A are maximized
Last Updated :
14 Sep, 2023
Given 2 integers A and B, the task is to increase A by at most B times such that zeroes at the end of A are maximized and print that final value of A. If 2 or more numbers have the same number of trailing zeroes, print the largest one.
Examples:
Input: A = 6, B = 11
Output: 60
Explanation: The answer would be 60, when we increase A by 10 times. Note that to get 2 zeroes at the end we need to increase A by at least 50 times. But we cannot do so as B = 11.
Input: A = 10050, B = 12345
Output: 120600000
Approach: To solve the problem follow the below idea.
- Let’s first count the degree of occurrence of 2 and 5 in the number A, denoted by Count2 and Count5 respectively. We can represent A as A = 2^Count2 * 5^Count5 * d, where d is not divisible by either 2 or 5.
- Let the answer be A*k. k ≤ B.
- We can increase Count2 or Count5 to get the A with the most trailing zeroes possible while spending the least possible k. For example, if Count2 < Count5, we can increase Count2 by 1 and multiply k by 2 as long as k*2 ≤ m and Count2 != Count5.
- We have Count2 = Count5, or k*5 > B, or k*2 > B. For the case Count2 = Count5, we multiply k by 10 as long as k*10 ≤ B.
- At this point, we have k*10 > B, so we find x = floor(B/k), which is a number between 1 and 9 (inclusive).
- Finally, we multiply k by x to get the desired answer, which is A*k.
Below are the steps for the above approach:
- Initialize a variable A0 to store the original value of A.
- Initialize two counters Count2 and Count5 to 0 to count the number of factors of 2 and 5 in A.
- Run a loop to check if A is divisible by 2, divide A by 2, and increment Count2.
- Similarly run a loop to check if A is divisible by 5, divide A by 5, and increment Count5.
- Initialize a variable k = 1.
- Run a loop to check whether Count2 is less than Count5 and whether k times 2 is less than or equal to B. If so, Count2 is incremented, and k is multiplied by 2.
- Similarly, run a loop to check whether Count5 is less than Count2 and whether k times 5 is less than or equal to B. If so, Count5 is incremented, and k is multiplied by 5.
- Run a loop to check whether k times 10 is less than or equal to B. If so, k is multiplied by 10.
- Check if k = 1, that means A0 times B is the optimal answer, otherwise, k is multiplied by B divided by k, to ensure that a result is a round number between 1 and 9.
- The final result is A0 * k.
Following is the code based on the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
int maximizeZeroes( int A, int B)
{
int A0 = A;
int Count2 = 0, Count5 = 0;
while (A > 0 && A % 2 == 0) {
A /= 2;
Count2++;
}
while (A > 0 && A % 5 == 0) {
A /= 5;
Count5++;
}
int k = 1;
while (Count2 < Count5 && k * 2 <= B) {
Count2++;
k *= 2;
}
while (Count5 < Count2 && k * 5 <= B) {
Count5++;
k *= 5;
}
while (k * 10 <= B) {
k *= 10;
}
if (k == 1) {
return (A0 * B);
}
else {
k *= B / k;
return (A0 * k);
}
}
int32_t main()
{
int A = 10050, B = 12345;
int ans = maximizeZeroes(A, B);
cout << ans << endl;
return 0;
}
|
Java
class GFG {
static int maximizeZeroes( int A, int B)
{
int A0 = A;
int Count2 = 0 , Count5 = 0 ;
while (A > 0 && A % 2 == 0 ) {
A /= 2 ;
Count2++;
}
while (A > 0 && A % 5 == 0 ) {
A /= 5 ;
Count5++;
}
int k = 1 ;
while (Count2 < Count5 && k * 2 <= B) {
Count2++;
k *= 2 ;
}
while (Count5 < Count2 && k * 5 <= B) {
Count5++;
k *= 5 ;
}
while (k * 10 <= B) {
k *= 10 ;
}
if (k == 1 ) {
return (A0 * B);
}
else {
k *= B / k;
return (A0 * k);
}
}
public static void main(String[] args)
{
int A = 10050 , B = 12345 ;
int ans = maximizeZeroes(A, B);
System.out.println(ans);
}
}
|
Python3
def maximizeZeroes(A: int , B: int ) - > int :
A0 = A
Count2 = 0
Count5 = 0
while A > 0 and A % 2 = = 0 :
A / / = 2
Count2 + = 1
while A > 0 and A % 5 = = 0 :
A / / = 5
Count5 + = 1
k = 1
while Count2 < Count5 and k * 2 < = B:
Count2 + = 1
k * = 2
while Count5 < Count2 and k * 5 < = B:
Count5 + = 1
k * = 5
while k * 10 < = B:
k * = 10
if k = = 1 :
return A0 * B
else :
k * = B / / k
return A0 * k
if __name__ = = '__main__' :
A = 10050
B = 12345
ans = maximizeZeroes(A, B)
print (ans)
|
C#
using System;
class GFG {
static long MaximizeZeroes( long A, long B)
{
long A0 = A;
long Count2 = 0, Count5 = 0;
while (A > 0 && A % 2 == 0) {
A /= 2;
Count2++;
}
while (A > 0 && A % 5 == 0) {
A /= 5;
Count5++;
}
long k = 1;
while (Count2 < Count5 && k * 2 <= B) {
Count2++;
k *= 2;
}
while (Count5 < Count2 && k * 5 <= B) {
Count5++;
k *= 5;
}
while (k * 10 <= B) {
k *= 10;
}
if (k == 1) {
return (A0 * B);
}
else {
k *= B / k;
return (A0 * k);
}
}
static void Main( string [] args)
{
long A = 10050, B = 12345;
long ans = MaximizeZeroes(A, B);
Console.WriteLine(ans);
}
}
|
Javascript
function maximizeZeroes(A, B) {
let A0 = A;
let Count2 = 0, Count5 = 0;
while (A > 0 && A % 2 === 0) {
A /= 2;
Count2++;
}
while (A > 0 && A % 5 === 0) {
A /= 5;
Count5++;
}
let k = 1;
while (Count2 < Count5 && k * 2 <= B) {
Count2++;
k *= 2;
}
while (Count5 < Count2 && k * 5 <= B) {
Count5++;
k *= 5;
}
while (k * 10 <= B) {
k *= 10;
}
if (k === 1) {
return (A0 * B);
}
else {
k *= Math.floor(B / k);
return (A0 * k);
}
}
let A = 10050, B = 12345;
let ans = maximizeZeroes(A, B);
console.log(ans);
|
Time Complexity: O(log(A) +log(B))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...