Open In App

Increase A by atmost B times so that zeroes at the end of A are maximized

Last Updated : 14 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
// Function to increase A by at most B
// times such that zeroes at the end
// of A are maximized
int maximizeZeroes(int A, int B)
{
 
    // Save the original value of A
    int A0 = A;
 
    // Count the number of 2s and 5s in A
    int Count2 = 0, Count5 = 0;
    while (A > 0 && A % 2 == 0) {
        A /= 2;
        Count2++;
    }
    while (A > 0 && A % 5 == 0) {
        A /= 5;
        Count5++;
    }
 
    // Choose k to maximize the number
    // of trailing zeroes in A
    // (i.e. answer is A.k)
    int k = 1;
 
    // Increase cnt2 or cnt5 to get the
    // most round number possible, while
    // spending the least possible k.
    while (Count2 < Count5 && k * 2 <= B) {
        Count2++;
        k *= 2;
    }
    while (Count5 < Count2 && k * 5 <= B) {
        Count5++;
        k *= 5;
    }
 
    // If we can add more trailing zeroes,
    // keep multiplying by 10.
    while (k * 10 <= B) {
        k *= 10;
    }
 
    // If k becomes 1, that means A0*B
    // is the optimal answer
    if (k == 1) {
        return (A0 * B);
    }
 
    // To maximize the output, as B/K
    // would be between 1 and 9
    else {
        k *= B / k;
        return (A0 * k);
    }
}
 
// Driver code
int32_t main()
{
    int A = 10050, B = 12345;
    int ans = maximizeZeroes(A, B);
 
    // Function call
    cout << ans << endl;
    return 0;
}


Java




// Java code for the above approach
 
class GFG {
    // Function to increase A by at most B
    // times such that zeroes at the end
    // of A are maximized
    static int maximizeZeroes(int A, int B)
    {
 
        // Save the original value of A
        int A0 = A;
 
        // Count the number of 2s and 5s in A
        int Count2 = 0, Count5 = 0;
        while (A > 0 && A % 2 == 0) {
            A /= 2;
            Count2++;
        }
        while (A > 0 && A % 5 == 0) {
            A /= 5;
            Count5++;
        }
 
        // Choose k to maximize the number
        // of trailing zeroes in A
        // (i.e. answer is A.k)
        int k = 1;
 
        // Increase cnt2 or cnt5 to get the
        // most round number possible, while
        // spending the least possible k.
        while (Count2 < Count5 && k * 2 <= B) {
            Count2++;
            k *= 2;
        }
        while (Count5 < Count2 && k * 5 <= B) {
            Count5++;
            k *= 5;
        }
 
        // If we can add more trailing zeroes,
        // keep multiplying by 10.
        while (k * 10 <= B) {
            k *= 10;
        }
 
        // If k becomes 1, that means A0*B
        // is the optimal answer
        if (k == 1) {
            return (A0 * B);
        }
 
        // To maximize the output, as B/K
        // would be between 1 and 9
        else {
            k *= B / k;
            return (A0 * k);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 10050, B = 12345;
        int ans = maximizeZeroes(A, B);
 
        // Function call
        System.out.println(ans);
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Python3




# Function to increase A by at most B
# times such that zeroes at the end
# of A are maximized
def maximizeZeroes(A: int, B: int) -> int:
 
    # Save the original value of A
    A0 = A
 
    # Count the number of 2s and 5s in 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
 
    # Choose k to maximize the number
    # of trailing zeroes in A
    # (i.e. answer is A.k)
    k = 1
 
    # Increase cnt2 or cnt5 to get the
    # most round number possible, while
    # spending the least possible k.
    while Count2 < Count5 and k * 2 <= B:
        Count2 += 1
        k *= 2
    while Count5 < Count2 and k * 5 <= B:
        Count5 += 1
        k *= 5
 
    # If we can add more trailing zeroes,
    # keep multiplying by 10.
    while k * 10 <= B:
        k *= 10
 
    # If k becomes 1, that means A0*B
    # is the optimal answer
    if k == 1:
        return A0 * B
 
    # To maximize the output, as B/K
    # would be between 1 and 9
    else:
        k *= B // k
        return A0 * k
 
# Driver code
if __name__ == '__main__':
    A = 10050
    B = 12345
    ans = maximizeZeroes(A, B)
 
    # Function call
    print(ans)


C#




using System;
 
class GFG {
    // Function to increase A by at most B
    // times such that zeroes at the end
    // of A are maximized
    static long MaximizeZeroes(long A, long B)
    {
        // Save the original value of A
        long A0 = A;
 
        // Count the number of 2s and 5s in A
        long Count2 = 0, Count5 = 0;
        while (A > 0 && A % 2 == 0) {
            A /= 2;
            Count2++;
        }
        while (A > 0 && A % 5 == 0) {
            A /= 5;
            Count5++;
        }
 
        // Choose k to maximize the number
        // of trailing zeroes in A
        // (i.e. answer is A.k)
        long k = 1;
 
        // Increase Count2 or Count5 to get the
        // most round number possible, while
        // spending the least possible k.
        while (Count2 < Count5 && k * 2 <= B) {
            Count2++;
            k *= 2;
        }
        while (Count5 < Count2 && k * 5 <= B) {
            Count5++;
            k *= 5;
        }
 
        // If we can add more trailing zeroes,
        // keep multiplying by 10.
        while (k * 10 <= B) {
            k *= 10;
        }
 
        // If k becomes 1, that means A0*B
        // is the optimal answer
        if (k == 1) {
            return (A0 * B);
        }
        // To maximize the output, as B/K
        // would be between 1 and 9
        else {
            k *= B / k;
            return (A0 * k);
        }
    }
 
    // Driver code
    static void Main(string[] args)
    {
        long A = 10050, B = 12345;
        long ans = MaximizeZeroes(A, B);
 
        // Function call
        Console.WriteLine(ans);
    }
}


Javascript




// Function to increase A by at most B
// times such that zeroes at the end
// of A are maximized
function maximizeZeroes(A, B) {
     
    // Save the original value of A
    let A0 = A;
     
    // Count the number of 2s and 5s in A
    let Count2 = 0, Count5 = 0;
    while (A > 0 && A % 2 === 0) {
        A /= 2;
        Count2++;
    }
    while (A > 0 && A % 5 === 0) {
        A /= 5;
        Count5++;
    }
     
     // Choose k to maximize the number
    // of trailing zeroes in A
    // (i.e. answer is A.k)
    let k = 1;
     
     
    // Increase cnt2 or cnt5 to get the
    // most round number possible, while
    // spending the least possible k.
    while (Count2 < Count5 && k * 2 <= B) {
        Count2++;
        k *= 2;
    }
    while (Count5 < Count2 && k * 5 <= B) {
        Count5++;
        k *= 5;
    }
     
    // If we can add more trailing zeroes,
    // keep multiplying by 10   
    while (k * 10 <= B) {
        k *= 10;
    }
     
     
    // If k becomes 1, that means A0*B
    // is the optimal answer
    if (k === 1) {
        return (A0 * B);
    }
     
    // To maximize the output, as B/K
    // would be between 1 and 9
    else {
        k *= Math.floor(B / k);
        return (A0 * k);
    }
}
 
// Driver code
let A = 10050, B = 12345;
let ans = maximizeZeroes(A, B);
 
console.log(ans);


Output

120600000

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



Similar Reads

Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
Given three integers X, Y and Z, the task is to find the sum of all the numbers formed having 4 at most X times, 5 at most Y times, and 6 at most Z times, under mod 10^9+7.Examples: Input: X = 1, Y = 1, Z = 1 Output: 3675 Explanation: 4 + 5 + 6 + 45 + 54 + 56 + 65 + 46 + 64 + 456 + 465 + 546 + 564 + 645 + 654 = 3675 Input: X = 4, Y = 5, Z = 6 Outpu
9 min read
Find zeroes to be flipped so that number of consecutive 1's is maximized
Given a binary array and an integer m, find the position of zeroes flipping which creates maximum number of consecutive 1's in array. Examples : Input: arr[] = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1} m = 2Output: 5 7We are allowed to flip maximum 2 zeroes. If we fliparr[5] and arr[7], we get 8 consecutive 1's which ismaximum possible under given constrai
19 min read
Set entire Matrix row and column as Zeroes (Set Matrix Zeroes)
Given a Matrix arr of size M x N, the task is to set all rows and columns to zeroes if a particular element is zero, in constant space complexity. [caption width="800"]Set Matrix Zeroes[/caption]Examples: Input: [ [1, 1, 1], [1, 0, 1], [1, 1, 1]]Output: [ [1, 0, 1], [0, 0, 0], [1, 0, 1]]Explanation: one zero is present at cell(2,2), and all the ele
20 min read
Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples: Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9} Output: 1 9 8 4 2 7 6 9 0 0 0 0 0 Algo
7 min read
Move all zeroes to end of array using Two-Pointers
Given an array of random numbers, Push all the zero’s of the given array to the end of the array. For example, if the given arrays is {1, 0, 2, 6, 0, 4}, it should be changed to {1, 2, 6, 4, 0, 0}. The order of all other elements should be the same.Examples: Input: arr[]={8, 9, 0, 1, 2, 0, 3} Output: arr[]={8, 9, 1, 2, 3, 0, 0} Explanation: Swap {0
6 min read
C++ Program to Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:   Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9} Output: 1 9 8 4 2 7 6 9 0 0 0 0 0
2 min read
Java Program to Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:   Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9} Output: 1 9 8 4 2 7 6 9 0 0 0 0 0
2 min read
Python3 Program to Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:   Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9} Output: 1 9 8 4 2 7 6 9 0 0 0 0 0
2 min read
Javascript Program to Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:   Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9} Output: 1 9 8 4 2 7 6 9 0 0 0 0 0
2 min read
POTD Solutions | 31 Oct’ 23 | Move all zeroes to end of array
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Two Pointers but will also help you build up problem-solving skills.[caption width="800"]POTD 31 Oct 2023
6 min read
Article Tags :
Practice Tags :