Maximize the product of digits by incrementing any digit X times
Last Updated :
24 Feb, 2023
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: Choose the first digit and increment it from 2 to 3, N = 3543
- Second operation: Choose the last digit and increment it from 3 to 4, N = 3544
- Third operation: Choose the last digit and increment it from 4 to 5, N = 3545
- Fourth operation: Choose 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:
C++
#include <bits/stdc++.h>
using namespace std;
long long Max_Product( int N, int X)
{
vector < int > digits;
int temp = N;
while (temp != 0) {
digits.push_back(temp % 10);
temp = temp / 10;
}
int length = digits.size();
cout<<length<< " " ;
sort(digits.begin(),digits.end());
for ( int i = 0; i < X; i++) {
if (digits[0] == 9)
break ;
digits[0] = digits[0] + 1;
sort(digits.begin(),digits.end());
}
long long Product = 1;
for ( int i = 0; i < length; i++) {
Product = Product * digits[i];
}
return Product;
}
int main() {
int N = 2543;
int X = 4;
cout<<Max_Product(N, X)<<endl;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class NewClass1 {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 2543 ;
int X = 4 ;
System.out.println(Max_Product(N, X));
}
static long Max_Product( int N, int X)
{
int length = Integer.toString(N).length();
int [] digits = new int [length];
int temp = N;
int counter = 0 ;
while (temp != 0 ) {
digits[counter] = temp % 10 ;
temp = temp / 10 ;
counter++;
}
Arrays.sort(digits);
for ( int i = 0 ; i < X; i++) {
if (digits[ 0 ] == 9 )
break ;
digits[ 0 ] = digits[ 0 ] + 1 ;
Arrays.sort(digits);
}
long Product = 1 ;
for ( int i = 0 ; i < length; i++) {
Product = Product * digits[i];
}
return Product;
}
}
|
Python3
from typing import List
def Max_Product(N: int , X: int ) - > int :
digits = []
temp = N
while temp ! = 0 :
digits.append(temp % 10 )
temp = temp / / 10
length = len (digits)
digits.sort()
for i in range (X):
if digits[ 0 ] = = 9 :
break
digits[ 0 ] + = 1
digits.sort()
Product = 1
for i in range (length):
Product * = digits[i]
return Product
if __name__ = = '__main__' :
N = 2543
X = 4
print (Max_Product(N, X))
|
C#
using System;
public class GFG {
static long Max_Product( int N, int X)
{
int length = N.ToString().Length;
int [] digits = new int [length];
int temp = N;
int counter = 0;
while (temp != 0) {
digits[counter] = temp % 10;
temp = temp / 10;
counter++;
}
Array.Sort(digits);
for ( int i = 0; i < X; i++) {
if (digits[0] == 9)
break ;
digits[0] = digits[0] + 1;
Array.Sort(digits);
}
long Product = 1;
for ( int i = 0; i < length; i++) {
Product = Product * digits[i];
}
return Product;
}
static public void Main()
{
int N = 2543;
int X = 4;
Console.WriteLine(Max_Product(N, X));
}
}
|
Javascript
function Max_Product( N, X)
{
let digits= new Array();
let temp = N;
while (temp != 0) {
digits.push(temp % 10);
temp = Math.floor(temp / 10);
}
let length = digits.length;
digits.sort();
for (let i = 0; i < X; i++) {
if (digits[0] == 9)
break ;
digits[0] = digits[0] + 1;
digits.sort();
}
let Product = 1;
for (let i = 0; i < length; i++) {
Product = Product * digits[i];
}
return Product;
}
let N = 2543;
let X = 4;
console.log(Max_Product(N, X));
|
Time Complexity: O(X*(Z*log(Z))), Where Z is equal to the number of digits in N.
Auxiliary Space: O(Z)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...