The Wayback Machine - https://web.archive.org/web/20230116063248/https://www.geeksforgeeks.org/maximize-the-product-of-digits-by-incrementing-any-digit-x-times/
Skip to content
Related Articles

Related Articles

Maximize the product of digits by incrementing any digit X times

Improve Article
Save Article
  • Last Updated : 08 Jan, 2023
Improve Article
Save Article

Given two integers N and X. Find the maximum product of digits of N, such that increment any digit by 1 at most X times ( chosen digit < 9)

Example:   

Input: N = 2, X = 2
Output: 4
Explanation:

  • First Operation: Increment 2 to 3.
  • Second Operation: Increment 3 to 4.

Only a single digit is there. Therefore, the output is 4.  

Input: N = 2543, X = 4
Output: 400
Explanation:

  • First operation: Chose the first digit and increment it from 2 to 3, N = 3543  
  • Second operation: Chose the last digit and increment it from 3 to 4, N = 3544
  • Third operation: Chose the last digit and increment it from 4 to 5, N = 3545
  • Fourth operation: Chose the first digit and increment it from 3 to 4. N = 4545

The final value of N after 4 operations is= 4543, whose digits give the product as 4*5*4*5=400. Which is the maximum possible. 

Approach: The problem can be solved based on the following idea:

The problem is based on Greedy logic and observation based. We can use the Greedy approach to maximize the product of digits. For more clarification see the Concept of approach section below. 

Concept of approach:

The problem is observation based and can be solved by using Greedy technique. Here the Greedy approach comes that, We should increment that digit of N, Which is currently smallest in all of the digits and that digit must be not equal to 9.
This approach can be implemented by looping N to X times and each iteration increment that digit, Which is currently smallest from all of the digits.            

Follow the steps to solve the above idea:

  • Store the digit of N in array digits[].
  • Sort the digits[].
  • If, the first element is not equal to 9, increment the digit by 1.
  • Again, sort the digits[] array.
  • If the first element is equal to 9, Break the loop.
  • After X iterations, Calculate the product of the digits array.

Below is the code to implement the approach:

Java

// Java code to implement the approach

import java.io.*;
import java.lang.*;
import java.util.*;

class NewClass1 {

    // Driver code
    public static void main(String[] args)
        throws java.lang.Exception
    {

        // Input value of N and X
        int N = 2543;
        int X = 4;

        // Function call
        System.out.println(Max_Product(N, X));
    }

    // Function for returing maximum
    // possible product
    static long Max_Product(int N, int X)
    {

        // Length of integer N
        int length = Integer.toString(N).length();

        // Array to store digits of N
        int[] digits = new int[length];

        // Temprorary variable to store
        // value of N
        int temp = N;

        // Counter variable
        int counter = 0;

        // Loop for initializing digits
        // of N in digit[] array
        while (temp != 0) {
            digits[counter] = temp % 10;
            temp = temp / 10;
            counter++;
        }

        // Sorting digits[]
        Arrays.sort(digits);

        // Looping X times
        for (int i = 0; i < X; i++) {

            // Condition when digits is
            // equal to 9 and then
            // breaking the loop
            if (digits[0] == 9)
                break;

            // Incrementing the smallest
            // digit of digits[]
            digits[0] = digits[0] + 1;

            // Sorting digits[] at each
            // iteration
            Arrays.sort(digits);
        }

        // Varibable to hold product
        // of digits
        long Product = 1;

        // Loop for calculating product of
        // digits by iterating digits[]
        for (int i = 0; i < length; i++) {

            Product = Product * digits[i];
        }

        // Returning maximum posible
        // product
        return Product;
    }
}
Output

400

Time Complexity: O(X*(Z*log(Z))), Where Z is equal to the number of digits in N.  
Auxiliary Space: O(Z)

Related Articles:

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!