Given an integer K, the task is to find the maximum possible number having digit sum K and no 0 and same digits consecutively.
Examples:
Input: K = 3
Output: 21
Explanation: All possible numbers giving digit sum 3 without zero are – 3, 12, 21, 111.
As 111 has consecutive 1s, it isn’t a valid answer.
Hence maximum number with digit sum 3 is 21.Input: K = 4
Output: 121
Explanation: All possible numbers giving digit sum 4 without zero are – 4, 13, 22, 31, 112, 121, 211, 1111.
As 1111, 211, has consecutive 1s, it isn’t a valid answer.
Hence maximum number with digit sum 4 is 121.
Approach: The problem can be solved using the following idea:
To get the maximum value it should have maximum number of digits. For that the least significant digits should be as minimum as possible without violating the conditions. So those can be filled in any of the following two ways:
- 1 then 2 repeatedly from the least significant position till the digit sum is K
- 2 and then 1 repeatedly from the least significant position till the digit sum is K
So to choose the proper sequence divide the sum by 3 and if the remainder is 1 or 0 then start filling with 1 from last, as the remaining sum will be 1 at end. Otherwise use 2nd sequence.
Follow the steps mentioned below to solve the problem:
- Divide the given number with 3.
- Fill as per the above observation based on the remainder.
- Return the number as the answer.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
int maxNum( int n)
{ // Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
cout << "1" ;
if (n > 0) {
n -= 2;
cout << "2" ;
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
cout << "2" ;
if (n > 0) {
n -= 1;
cout << "1" ;
}
}
}
} // Driver code int main()
{ int K = 4;
maxNum(K);
return 0;
} |
// Java implementation of the approach import java.io.*;
class GFG {
static void maxNum( int n)
{
// Dividing n by 3
if (n % 3 == 1 ) {
while (n > 0 ) {
// Printing 1s and 2s
// and subtracting from n
n -= 1 ;
System.out.print( "1" );
if (n > 0 ) {
n -= 2 ;
System.out.print( "2" );
}
}
}
else {
while (n > 0 ) {
// Printing 2s and 1s and
// subtracting from n
n -= 2 ;
System.out.print( "2" );
if (n > 0 ) {
n -= 1 ;
System.out.print( "1" );
}
}
}
}
// Driver code
public static void main (String[] args) {
int K = 4 ;
maxNum(K);
}
} // This code is contributed by hrithikgarg03188. |
# Python 3 implementation of the approach def maxNum(n):
# Dividing n by 3
if (n % 3 = = 1 ):
while (n > 0 ):
# Printing 1s and 2s
# and subtracting from n
n - = 1
print ( "1" , end = "")
if (n > 0 ):
n - = 2
print ( "2" , end = "")
else :
while (n > 0 ):
# Printing 2s and 1s and
# subtracting from n
n - = 2
print ( "2" , end = "")
if (n > 0 ):
n - = 1
print ( "1" , end = "")
# Driver code if __name__ = = "__main__" :
K = 4
maxNum(K)
# This code is contributed by ukasp.
|
// C# implementation of the approach using System;
class GFG {
static void maxNum( int n)
{
// Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
Console.Write( "1" );
if (n > 0) {
n -= 2;
Console.Write( "2" );
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
Console.Write( "2" );
if (n > 0) {
n -= 1;
Console.Write( "1" );
}
}
}
}
// Driver code
public static void Main()
{
int K = 4;
maxNum(K);
}
} // This code is contributed by Samim Hossain Mondal. |
<script> // JavaScript implementation of the approach
const maxNum = (n) => {
// Dividing n by 3
if (n % 3 == 1) {
while (n > 0) {
// Printing 1s and 2s
// and subtracting from n
n -= 1;
document.write( "1" );
if (n > 0) {
n -= 2;
document.write( "2" );
}
}
}
else {
while (n > 0) {
// Printing 2s and 1s and
// subtracting from n
n -= 2;
document.write( "2" );
if (n > 0) {
n -= 1;
document.write( "1" );
}
}
}
}
// Driver code
let K = 4;
maxNum(K);
// This code is contributed by rakeshsahni </script> |
121
Time complexity: O(K)
Auxiliary Space: O(1)