Maximum number with digit sum K and no 0s or duplicate adjacent digits
Last Updated :
13 May, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int maxNum( int n)
{
if (n % 3 == 1) {
while (n > 0) {
n -= 1;
cout << "1" ;
if (n > 0) {
n -= 2;
cout << "2" ;
}
}
}
else {
while (n > 0) {
n -= 2;
cout << "2" ;
if (n > 0) {
n -= 1;
cout << "1" ;
}
}
}
}
int main()
{
int K = 4;
maxNum(K);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void maxNum( int n)
{
if (n % 3 == 1 ) {
while (n > 0 ) {
n -= 1 ;
System.out.print( "1" );
if (n > 0 ) {
n -= 2 ;
System.out.print( "2" );
}
}
}
else {
while (n > 0 ) {
n -= 2 ;
System.out.print( "2" );
if (n > 0 ) {
n -= 1 ;
System.out.print( "1" );
}
}
}
}
public static void main (String[] args) {
int K = 4 ;
maxNum(K);
}
}
|
Python3
def maxNum(n):
if (n % 3 = = 1 ):
while (n > 0 ):
n - = 1
print ( "1" , end = "")
if (n > 0 ):
n - = 2
print ( "2" , end = "")
else :
while (n > 0 ):
n - = 2
print ( "2" , end = "")
if (n > 0 ):
n - = 1
print ( "1" , end = "")
if __name__ = = "__main__" :
K = 4
maxNum(K)
|
C#
using System;
class GFG {
static void maxNum( int n)
{
if (n % 3 == 1) {
while (n > 0) {
n -= 1;
Console.Write( "1" );
if (n > 0) {
n -= 2;
Console.Write( "2" );
}
}
}
else {
while (n > 0) {
n -= 2;
Console.Write( "2" );
if (n > 0) {
n -= 1;
Console.Write( "1" );
}
}
}
}
public static void Main()
{
int K = 4;
maxNum(K);
}
}
|
Javascript
<script>
const maxNum = (n) => {
if (n % 3 == 1) {
while (n > 0) {
n -= 1;
document.write( "1" );
if (n > 0) {
n -= 2;
document.write( "2" );
}
}
}
else {
while (n > 0) {
n -= 2;
document.write( "2" );
if (n > 0) {
n -= 1;
document.write( "1" );
}
}
}
}
let K = 4;
maxNum(K);
</script>
|
Time complexity: O(K)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...