Improve Article

Save Article

Like Article

Improve Article

Save Article

Like Article

Given 2 integers **A **and **B,** The task is to summation A by astatine astir B times specified that zeroes astatine The extremity of A are maximized and people that last worth of A. If 2 aliases much numbers person The aforesaid number of trailing zeroes, people The largest one.

**Examples:**

**Input: **A = 6, B = 11**Output: **60**Explanation: **The reply would beryllium 60, erstwhile we summation A by 10 times. Note that to get 2 zeroes astatine The extremity we request to summation A by astatine slightest 50 times. But we cannot do truthful arsenic B = 11.

**Input: **A = 10050, B = 12345**Output: **120600000

**Approach: **To lick The problem travel The beneath idea.

- Let’s first count The grade of occurrence of 2 and 5 in The number A, denoted by Count2 and Count5 respectively. We Can correspond A arsenic A = 2^Count2 * 5^Count5 * d, wherever d is not divisible by either 2 aliases 5.
- Let The reply beryllium A*k. k ≤ B.
- We Can summation Count2 aliases Count5 to get The A pinch The astir trailing zeroes imaginable while spending The slightest imaginable k. For example, if Count2 < Count5, we Can summation Count2 by 1 and multiply k by 2 arsenic agelong arsenic k*2 ≤ m and Count2 != Count5.
- We person Count2 = Count5, aliases k*5 > B, aliases k*2 > B. For The lawsuit Count2 = Count5, we multiply k by 10 arsenic agelong arsenic k*10 ≤ B.
- At this point, we person k*10 > B, truthful we find x = floor(B/k), which is simply a number betwixt 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 supra approach:

- Initialize a adaptable A0 to shop The original worth of A.
- Initialize 2 counters Count2 and Count5 to 0 to count The number of factors of 2 and 5 in A.
- Run a loop to cheque if A is divisible by 2, disagreement A by 2, and increment Count2.
- Similarly tally a loop to cheque if A is divisible by 5, disagreement A by 5, and increment Count5.
- Initialize a adaptable k = 1.
- Run a loop to cheque whether Count2 is little than Count5 and whether k times 2 is little than aliases adjacent to B. If so, Count2 is incremented, and k is multiplied by 2.
- Similarly, tally a loop to cheque whether Count5 is little than Count2 and whether k times 5 is little than aliases adjacent to B. If so, Count5 is incremented, and k is multiplied by 5.
- Run a loop to cheque whether k times 10 is little than aliases adjacent to B. If so, k is multiplied by 10.
- Check if k = 1, that intends A0 times B is The optimal answer, otherwise, k is multiplied by B divided by k, to guarantee that a consequence is simply a information number betwixt 1 and 9.
- The last consequence is A0 * k.

Following is The codification based connected The supra attack :

## C++

#include <bits/stdc++.h>

using namespace std;

#define int agelong 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;

}

**Time Complexity: **O(log(A) +log(B))**Auxiliary Space: **O(1)

Like Article

Save Article

**Editor:**Naga

**Beritaja.com**at