Minimize the number formed by replacing adjacent pair of digits with their sum
Last Updated :
10 Feb, 2022
Given string s denoting a number. The task is to find the minimum number that can be formed after replacing two consecutive digits of s by their sum.
Examples:
Input: s = “1005”
Output: 105
Explanation: Select and replace any two consecutive digits with its sum
Input: s = “56773″
Output: 11773
Explanation: Select and replace first two consecutive digits (5 and 6) with its sum (11)
Approach: This problem can be solved by using the Greedy Approach. Use the following observation:
If there exists at least one pair having sum less than 10:
- The sum will always be greater than or equal to the elements of the adjacent pair.
- So replacing the last arrival of such pair will give the minimum number possible. Otherwise, the number will increase.
- For example, 2381 can be made 581 or 239 and 239 is the minimum between these two.
- This condition is given priority because it decreases the number of digits in the number and makes the value less than any number having same number of digits as the actual number.
If there is no such pair having sum less than 10:
- The sum will always be less than the number formed by the adjacent digits.
- So replacing the first occurrence of such a pair will give the minimum number as it makes the most significant part minimum.
- For example, 386 can be made 116 or 314. 116 is the lesser one.
Follow the steps below to solve the given problem using the above observation.
- Firstly assume that there exists at least one pair of consecutive elements whose sum is less than 10.
- Iterate the numeric string from the back.
- Find the first pair of consecutive digits whose sum is less than 10.
- Replace them with their sum and return the string.
- Now, handle cases in which there is not a single pair of consecutive elements whose sum is less than 10.
- Iterate the numeric string from the start.
- The first pair of consecutive digits whose sum is equal to or greater than 10 is the optimal answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSmallestNumber(string str, int N)
{
string ans = "" ;
bool found = false ;
for ( int i = N - 1; i >= 1; i--) {
int a = (str[i] - '0' );
int b = (str[i - 1] - '0' );
if (a + b < 10) {
found = true ;
ans = str.substr(0, i - 1);
int sum = a + b;
int last = sum % 10;
char l = last + '0' ;
sum /= 10;
if (sum != 0) {
char p = sum + '0' ;
ans.push_back(p);
}
ans.push_back(l);
ans += str.substr(i + 1,
N - i - 1);
cout << ans;
break ;
}
}
if (!found) {
int sum = (str[0] - '0' )
+ (str[1] - '0' );
ans = ( char )(sum / 10 + '0' );
sum %= 10;
ans += (sum + '0' );
ans += str.substr(2, N - 2);
cout << ans;
}
}
int main()
{
string s = "56773" ;
int N = s.length();
findSmallestNumber(s, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findSmallestNumber(String str, int N)
{
String ans = "" ;
boolean found = false ;
for ( int i = N - 1 ; i >= 1 ; i--) {
int a = (str.charAt(i) - '0' );
int b = (str.charAt(i- 1 )- '0' );
if (a + b < 10 ) {
found = true ;
ans = str.substring( 0 , i - 1 );
int sum = a + b;
int last = sum % 10 ;
char l = ( char )(last + '0' );
sum /= 10 ;
if (sum != 0 ) {
char p = ( char )(sum + '0' );
ans += p;
}
ans += l;
ans += str.substring(i + 1 ,
N - i - 1 );
System.out.println(ans);
break ;
}
}
if (!found) {
int sum = (str.charAt( 0 ) - '0' )
+ (str.charAt( 1 ) - '0' );
Integer num = sum / 10 ;
ans = num.toString();
Integer summ = sum % 10 ;
ans += summ.toString();
ans += str.substring( 2 , N );
System.out.println(ans);
}
}
public static void main(String args[])
{
String s = "56773" ;
int N = s.length();
findSmallestNumber(s, N);
}
}
|
Python3
def findSmallestNumber( str , N):
ans = ""
found = False
for i in range (N - 1 , 1 , - 1 ):
a = ( ord ( str [i]) - ord ( '0' ))
b = ( ord ( str [i - 1 ]) - ord ( '0' ))
if (a + b < 10 ):
found = True
ans = str [ 0 : i - 1 ]
sum = a + b
last = sum % 10
l = ( str )(last)
sum = sum / / 10
if ( sum ! = 0 ):
p = ( str )( sum )
ans + = p
ans + = (l)
ans + = str [i + 1 : i + 1 + N - i - 1 ]
print (ans)
break
if ( not found):
sum = ( ord ( str [ 0 ]) - ord ( '0' )) + ( ord ( str [ 1 ]) - ord ( '0' ))
ans = f '{sum // 10}'
sum % = 10
ans + = f '{sum}'
ans + = str [ 2 : 2 + N - 2 ]
print (ans)
s = "56773"
N = len (s)
findSmallestNumber(s, N)
|
C#
using System;
using System.Collections;
class GFG
{
static void findSmallestNumber( string str, int N)
{
string ans = "" ;
bool found = false ;
for ( int i = N - 1; i >= 1; i--) {
int a = (str[i] - '0' );
int b = (str[i - 1] - '0' );
if (a + b < 10) {
found = true ;
ans = str.Substring(0, i - 1);
int sum = a + b;
int last = sum % 10;
char l = ( char )(last + '0' );
sum /= 10;
if (sum != 0) {
char p = ( char )(sum + '0' );
ans += p;
}
ans += l;
ans += str.Substring(i + 1,
N - i - 1);
Console.Write(ans);
break ;
}
}
if (!found) {
int sum = (str[0] - '0' )
+ (str[1] - '0' );
int num = sum / 10;
ans = num.ToString();
sum %= 10;
ans += sum.ToString();
ans += str.Substring(2, N - 2);
Console.Write(ans);
}
}
public static void Main()
{
string s = "56773" ;
int N = s.Length;
findSmallestNumber(s, N);
}
}
|
Javascript
<script>
const findSmallestNumber = (str, N) => {
let ans = "" ;
let found = false ;
for (let i = N - 1; i >= 1; i--) {
let a = (str.charCodeAt(i) - '0' .charCodeAt(0));
let b = (str.charCodeAt(i - 1) - '0' .charCodeAt(0));
if (a + b < 10) {
found = true ;
ans = str.substring(0, i - 1);
let sum = a + b;
let last = sum % 10;
let l = last.toString();
sum = parseInt(sum / 10);
if (sum != 0) {
let p = sum.toString();
ans += p;
}
ans += (l);
ans += str.substring(i + 1, i + 1 + N - i - 1);
document.write(ans);
break ;
}
}
if (!found) {
let sum = (str.charCodeAt(0) - '0' .charCodeAt(0))
+ (str.charCodeAt(1) - '0' .charCodeAt(0));
ans = (parseInt(sum / 10)).toString();
sum %= 10;
ans += sum.toString();
ans += str.substring(2, 2 + N - 2);
document.write(ans);
}
}
let s = "56773" ;
let N = s.length;
findSmallestNumber(s, N);
</script>
|
Time Complexity: O(N) where N is the size of the string
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...