Max Difference You Can Get From Changing an Integer in C++


Suppose we have an integer num. We will apply the following steps exactly two times, the steps are like −

  • Pick a digit x in range 0 to 9.

  • Pick another digit y also in range 0 to 9. The digit y can be equal to x.

  • Replace all the occurrences of x in the decimal representation of num by y. The new integer cannot have any leading zeros, also the new integer cannot be of value 0.

Now consider a and b be the results of applying the operations to num the first and second times, respectively. Then find the max difference between a and b.

So, if the input is like 555, then the output will be 888 as the first-time pick x = 5 and y = 9 and store the new integer in a. The second time pick x = 5 and y = 1 and store the new integer in b.

So we have now a = 999 and b = 111 and maximum difference is = 999 - 111 = 888.

To solve this, we will follow these steps −

  • Define a function getMax(), this will take x,

  • s := convert x to string

  • a := '9'

  • i := 0

  • while (i < size of s and s[i] is same as '9'), do −

    • (increase i by 1)

  • if i < size of s, then −

    • a := s[i]

  • for initialize i := 0, when i < size of s, update (increase i by 1), do −

    • if s[i] is same as a, then −

      • s[i] := '9'

  • return s as number

  • Define a function getMin(), this will take x,

  • s := convert x to string

  • if s[0] is not equal to '1', then −

    • a := s[0]

    • for initialize i := 0, when i < size of s, update (increase i by 1), do −

      • if s[i] is same as a, then −

        • s[i] := '1'

  • Otherwise

    • if size of s is same as 1, then −

      • return 1

    • i := 0

    • a := '1'

    • while (i < size of s and s[i] <= '1'), do −

      • (increase i by 1)

    • if i < size of s, then −

      • a := s[i]

    • for initialize i := 1, when i < size of s, update (increase i by 1), do −

      • if s[i] is same as a, then −

        • s[i] := '0'

  • return s as number

  • From the main method do the following −

  • a := getMax(num)

  • b := getMin(num)

  • return |a - b|

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int getMax(int x){
      string s = to_string(x);
      char a = '9', b;
      int i = 0;
      while (i < s.size() && s[i] == '9')
         i++;
      if (i < s.size())
         a = s[i];
      for (int i = 0; i < s.size(); i++) {
         if (s[i] == a) {
            s[i] = '9';
         }
      }
      return stoi(s);
   }
   int getMin(int x){
      string s = to_string(x);
      char a;
      if (s[0] != '1') {
         a = s[0];
         for (int i = 0; i < s.size(); i++) {
            if (s[i] == a) {
               s[i] = '1';
            }
         }
      }
      else {
         if (s.size() == 1) {
            return 1;
         }
         int i = 0;
         a = '1';
         while (i < s.size() && s[i] <= '1')
            i++;
         if (i < s.size())
            a = s[i];
         for (int i = 1; i < s.size(); i++) {
            if (s[i] == a) {
               s[i] = '0';
            }
         }
      }
      return stoi(s);
   }
   int maxDiff(int num) {
      int a = getMax(num);
      int b = getMin(num);
      return abs(a - b);
   }
};
main(){
   Solution ob;
   cout << (ob.maxDiff(666));
}

Input

666

Output

888

Updated on: 17-Nov-2020

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements