How to proceed to find the maximum sum of 2 integers with replacing only one digit?

Viewed 374

example 1

a=1
b=90
answer will be 1+99 = 100

example 2

23
45
answer will be 93+45 =138

Note:there can be negative numbers also. You are not allowed to add digits, just replace a single digit to get maximum sum

4 Answers

Use this

 if(a>b || a==b){
   a=10*((a/10)+1)
   return a;
}
else{
b=10*((b/10)+1)
return b;
}

Let's assume that the first number has d1 digits, the second has d2 digits and for the sake of simplicity, let's assume further that

d1 >= d2

and

k = d1 - d2

so, k >= 0. If the first k digits of the larger number are modifiable (modifiable: if the number is positive, but the digit is not 9, or the number is negative), then modify that digit optimally, as described earlier.

Otherwise, in subsequent digits check whether any of the digits is modifiable and if so, compute the difference between the changes you would do on the two digits and choose the one where the change was larger.

When the first modification was done, then the work should stop.

Here is a straightforward recursion in JavaScript (easily translatable to Java or C++). The idea is to choose the best addition or subtraction (for negatives) possible for each number. The complexity is O(log10 n), where n is the larger number.

function improve(n){
  if (n > -10 && n < 10)
    return n < 0 ? -n : 9 - n;
    
  return Math.max(
    10 * improve(~~(n / 10)),
    n < 10 ? -(n % 10) : 9 - (n % 10)
  );
}

function f(a, b){
  return a + b + Math.max(improve(a), improve(b));
}

var abs = [
  [1, 90],
  [23, 45],
  [-94, 5]
];

for (let [a, b] of abs){
  console.log(a, b);
  console.log(f(a, b));
  console.log('');
}

I had a rather simple idea. First convert two integers `n1`, `n2` into c-string `s1` , `s2`. Then if s1[0] = '-' (n1 is negative) change `s1[1] = 0`, else (n1>0) change `s1[0] = 9`. Similarly for c-string `s2`. Finally compare which sum is the larger: `n1 + stoi(s2)` or `n2 + stoi(s1)` to determine the set to be choose.

One extra care is that for a integer >0, and starting with digits`999...` To take this case into consideration, we use a for loop to change the first digit that is not equal to `9`. If all digits are `9`, we do nothing to the integer.

#include <iostream>
#include <fstream>
#include <cstring>

int main()
{
    int n1, n2, a, b;
    char s1[32], s2[32];
    while (1)
     {
       std::cout << "\n input n1 & n2 = ";
       std::cin >> n1 >> n2;
       itoa(n1, s1, 10);
       itoa(n2, s2, 10);
       if (s1[0] == '-') s1[1] = '0';
      else for (int i=0; i<strlen(s1); i++) {
              if (s1[i] = '9') continue;
              else {s1[i] = '9'; break;}
       if (s2[0] == '-') s2[1] = '0';
      else for (int i=0; i<strlen(s2); i++) {
              if (s2[i] = '9') continue;
              else {s2[i] = '9'; break;}
       a = n1 + std::stoi(s2);
       b = n2 + std::stoi(s1);
       if (a > b) std::cout << "two integers: " << n1 << ", " << s2 <<std::endl;
       else  std::cout << "two integers: " << s1 << ", " << n2 <<std::endl;
     }
   return 0;
}

Some test sets:

$ ./a.exe

 input n1 & n2 = 12 78
 two integers: 92, 78

 input n1 & n2 = -45 90
 two integers: -05, 90
 
 input n1 & n2 = -34 -78
 two integers: -34, -08

 input n1 & n2 = 23 9999
 two integers: 93, 9999
Related