Maximize the number formed by replacing adjacent digits with their sum
Given a number N without leading 0s, the task is to find the maximum number formed after replacing any two adjacent digits by their sum only once.
Examples:
Input: 10057
Output: 10012
Explanation: 10012 is the maximum number that we can form since 5+7=12.
Input: 30
Output: 3 .
Explanation: 3+0=3 the only option .
Approach: The problem can be solved based on the following observation:
There can be two cases:
If there is at least one pair having sum greater than or equal to 10:
- The sum of the numbers will always be less than the number formed by the two adjacent digits but the number of digits will be same.
- So find such a pair that is at the right end of the number and replace the rightmost such pair.
If there is no such pair:
- Then the newly formed number will always have on less digit than N.
- To maximize the number it is optimal to replace the leftmost such pair because the sum will always be greater than any of the digits.
Follow the steps mentioned below to implement the idea:
- First of all, we will convert the given number to a string for sake of convenience. Let the string be s.
- Consider there is a pair whose sum is greater than 10:
- Traverse from the back.
- If such a pair is found replace them with the sum of those digits.
- Otherwise, there is no such pair.
- Replace the leftmost pair.
- The leftmost pair is formed by the first two digits. So replace them with their sum.
- The number formed is the required answer.
Below is the implementation of the above approach :
C++14
#include <bits/stdc++.h>
using namespace std;
string compute( int N)
{
string s = to_string(N);
int n = s.size();
bool f = 0;
int mx = 0, pos = -1;
string ans;
if (n < 2)
return ans;
for ( int i = 0; i < n - 1; i++) {
int k = s[i] - '0' ;
int p = s[i + 1] - '0' ;
int num = k + p;
if (num >= 10) {
f = 1;
mx = num;
pos = i;
}
}
if (f) {
for ( int i = 0; i <= pos - 1; i++)
ans += s[i];
ans += to_string(mx);
for ( int i = pos + 2; i < n; i++)
ans += s[i];
}
else {
int k = s[0] - '0' ;
int p = s[1] - '0' ;
int temp = k + p;
ans += to_string(temp);
for ( int i = 2; i < n; i++)
ans += s[i];
}
return ans;
}
int main()
{
int N = 10057;
cout << compute(N) << endl;
return 0;
}
|
Java
class GFG {
static String compute( int N)
{
String s = Integer.toString(N);
int n = s.length();
boolean f = false ;
int mx = 0 , pos = - 1 ;
String ans= "" ;
if (n < 2 )
return ans;
for ( int i = 0 ; i < n - 1 ; i++) {
int k = s.charAt(i)- '0' ;
int p = s.charAt(i+ 1 ) - '0' ;
int num = k + p;
if (num >= 10 ) {
f = true ;
mx = num;
pos = i;
}
}
if (f) {
for ( int i = 0 ; i <= pos - 1 ; i++){
ans = ans+s.charAt(i);
}
ans += Integer.toString(mx);
for ( int i = pos + 2 ; i < n; i++)
ans += s.charAt(i);
}
else {
int k = s.charAt( 0 ) - '0' ;
int p = s.charAt( 1 ) - '0' ;
int temp = k + p;
ans += String.valueOf(temp);
for ( int i = 2 ; i < n; i++)
ans += s.charAt(i);
}
return ans;
}
public static void main(String[] args) {
int N = 10057 ;
System.out.println(compute(N));
}
}
|
Python3
def compute(N) - > str :
s = str (N)
n = len (s)
f = False
mx = 0
pos = - 1
ans = ""
if (n < 2 ):
return ans
for i in range ( 0 ,n - 1 ):
k = ord (s[i]) - ord ( '0' )
p = ord (s[i + 1 ]) - ord ( '0' )
num = k + p
if (num > = 10 ):
f = True
mx = num
pos = i
if (f):
for i in range ( 0 ,pos):
ans + = s[i]
ans + = str (mx)
for i in range (pos + 2 ,n):
ans + = s[i]
else :
k = ord (s[ 0 ]) - ord ( '0' )
p = ord (s[ 1 ]) - ord ( '0' )
temp = k + p
ans + = str (temp)
for i in range ( 2 ,n):
ans + = s[i]
return ans
if __name__ = = '__main__' :
N = 10057
print (compute(N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string compute( int N)
{
string s = N.ToString();
int n = s.Length;
bool f = false ;
int mx = 0, pos = -1;
string ans = " " ;
if (n < 2)
return ans;
for ( int i = 0; i < n - 1; i++) {
int k = s[i] - '0' ;
int p = s[i + 1] - '0' ;
int num = k + p;
if (num >= 10) {
f = true ;
mx = num;
pos = i;
}
}
if (f) {
for ( int i = 0; i <= pos - 1; i++)
ans += s[i];
ans += mx.ToString();
for ( int i = pos + 2; i < n; i++)
ans += s[i];
}
else {
int k = s[0] - '0' ;
int p = s[1] - '0' ;
int temp = k + p;
ans += temp.ToString();
for ( int i = 2; i < n; i++)
ans += s[i];
}
return ans;
}
public static void Main( string [] args)
{
int N = 10057;
Console.WriteLine(compute(N));
}
}
|
Javascript
<script>
function compute(N)
{
let s = N.toString();
let n = s.length;
let f = 0;
let mx = 0, pos = -1;
let ans = "" ;
if (n < 2)
return ans;
for (let i = 0; i < n - 1; i++) {
let k = s[i].charCodeAt(0) - '0' .charCodeAt(0);
let p = s[i + 1].charCodeAt(0) - '0' .charCodeAt(0);
let num = k + p;
if (num >= 10) {
f = 1;
mx = num;
pos = i;
}
}
if (f) {
for (let i = 0; i <= pos - 1; i++)
ans += s[i];
ans += (mx).toString();
for (let i = pos + 2; i < n; i++)
ans += s[i];
}
else {
let k = s[0].charCodeAt(0) - '0' - charCodeAt(0);
let p = s[1].charCodeAt(0) - '0' .charCodeAt(0);
let temp = k + p;
ans += (temp).toString();
for (let i = 2; i < n; i++)
ans += s[i];
}
return ans;
}
let N = 10057;
document.write(compute(N));
</script>
|
Time Complexity: O(M) where M is the number of digits in the number.
Auxiliary Space: O(M)
Last Updated :
07 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...