Largest number not exceeding N that does not contain any of the digits of S


The challenge of finding the largest number not exceeding a given number N and not containing any of the digits in a string S is a problem that involves string manipulation and number theory. The goal is to determine the greatest possible number that is less than or equal to N while also excluding all of the digits found in the string S.

For instance, consider a scenario where N is equal to 1000 and S is equal to "42". In this scenario, the largest number not exceeding N and not containing any of the digits in S would be 999. This is because 999 is the largest possible number that can be formed by using the digits 0, 1, 3, 5, 6, 7, 8, and 9, without including the digits 4 and 2 from the string S.

Different approaches can be used to solve this problem, such as iterating through all numbers up to N and verifying if their digits are not present in S, or by utilizing more complex methods like dynamic programming or backtracking.

Algorithm

Step 1 − We will declare 2 variables named ‘N’ and ‘S’ as string in main() function.

Step 2 − We will pass these 2 variables as parameters for the LargestNumberFinder() function.

Step 3 − We will convert the string number N and S into integer implicitly to do mathematical operations such as comparision.

Step 4 − We will remove the leading 0’s in our number stored in N either manually typing them out or by creating a function which will do the same every time.

Step 5 − Then, we will start comparing the digits of the both the strings and finding out which is the largest number formed not more than ‘N’ that doesn’t contain any digit from string ‘S’.

Approach 1: - Naïve Approach

The basic method for finding the largest number in a given string using all digits present in another string is as follows. The main function declares the variables and calls the LargestNumberFinder function. This function takes two strings as inputs, checks for each value less than N that has all its digits present in string S. If the condition is met, the value is returned in string format. The attendance function is utilized to determine if the value stored in 'i' is part of string S while converting S into an integer data type. The input strings are converted into integers and loops are employed to evaluate the conditions. The code outputs the largest number in the given string that has all its digits present in another string.

Example

The code is a solution that finds the largest number smaller than N (input string converted to integer) composed of digits found in string S. The code utilizes two functions, 'attendance' and 'LargestNumberFinder', to determine and return the largest number. The attendance function takes an integer 'i' and string 's' as inputs, checks if the value stored in 'i' is part of string 's', and converts 's' into an integer data type. The LargestNumberFinder function takes two strings as inputs, 'x' and 's', converts 'x' into an integer, and then checks for each value less than N with all digits present in 's' using the attendance function. The main function declares the variables and invokes the LargestNumberFinder function, which returns the largest number as a string.

#include <iostream>
#include <string>
#include <vector>

// function to check whether value stored in ‘i’ is part of string S while also converting S into integer data type.
bool attendance(int i, std::string s) {
   while (i) {
      int first_digit = i % 10;
      i /= 10;
      int t = std::stoi(s);
      bool found = false;
      while (t) {
         int second_digit = t % 10;
         t /= 10;
         if (second_digit == first_digit) {
            found = true;
            break;
         }
      }
      if (!found)
         return false;
   }
   return true;
}

// function to input two strings and check for each value less than N with all digits present in S.
std::string LargestNumberFinder(std::string x, std::string s) {
   int N = std::stoi(x);
   for (int i = N; i >= 1; i--) {
      if (attendance(i, s)) {
         return std::to_string(i);
      }
   }
   return "-1";
}

// main function to declare the variables and call the function.
int main() {
   std::string N = "100709";
   std::string S = "70";
   std::cout << LargestNumberFinder(N, S);
}

Output

77777

Approach 2:- Efficient Approach

The approach 2 for the problem of finding the largest possible number formed by replacing the digits of a given number string N with the digits of a given string S is an efficient approach. This approach first checks for the presence of each digit of N in S and replaces the first digit found in S with the largest digit not present in S. The rest of the digits are then replaced with the largest digit not present in S. The leading zeros are then removed and the result is returned as the largest number possible. This approach is more efficient than the previous approach as it does not require sorting the strings.

Example

The code solves a problem of finding the greatest number that can be formed from a given string "N" by replacing a digit with the highest digit not present in the string "S". The code utilizes an efficient method to solve the problem. The LargestNumberFinder function takes two string inputs, "num" and "s", and returns the largest possible number. The vector "vis_s" is utilized to store the values of string "s". The code first identifies the first digit of string "num" that is part of string "s". Then it swaps that digit with the highest digit not present in string "s". The code then finds the highest digit not found in string "s" and replaces the rest of the digits in string "num" with that digit. The leading zeros are removed from the final string, and if the string is empty, the function returns "0". The code outputs the result by calling the function with inputs "N" and "S".

#include <iostream>
#include <string>
#include <vector>

using namespace std;

// function to check for all values of String N with String S and replacing the digit if found same with the largest possible digit not present in S.
string LargestNumberFinder(string num, string s) {
   vector<bool> vis_s(10, false);
   for (int i = 0; i < (int)s.size(); i++) {
      vis_s[int(s[i]) - 48] = true;
   }
   int n = num.size();
   int in = -1;
   for (int i = 0; i < n; i++) {
      if (vis_s[(int)num[i] - '0']) {
         in = i;
         break;
      }
   }
   if (in == -1) {
      return num;
   }
   for (char dig = num[in]; dig >= '0'; dig--) {
      if (vis_s[(int)dig - '0'] == 0) {
         num[in] = dig;
         break;
      }
   }
   char LargestDig = '0';
   for (char dig = '9'; dig >= '0'; dig--) {
      if (vis_s[dig - '0'] == false) {
         LargestDig = dig;
         break;
      }
   }
   for (int i = in + 1; i < n; i++) {
      num[i] = LargestDig;
   }
   int Count = 0;
   for (int i = 0; i < n; i++) {
      if (num[i] == '0')
         Count++;
      else
         break;
   }
   num.erase(0, Count);
   if ((int)num.size() == 0)
      return "0";
   return num;
}
int main() {
   string N = "161516";
   string S = "756";
   cout << LargestNumberFinder(N, S);
   return 0;
}

Output

149999

Conclusion

Through this article, we got one step closer to understand the why behind such problems and understood the concepts which will help us use these basic concepts in major real life problems which were mentioned in the beginning. Like, in our code we tackled each problem separately and then sewed the code together like a beautiful handiwork similarly, we will use this concept and try to solve problems one by one. We will as usual will begin with the Naïve approach but with keen eye and effort we will find a much more efficient way. Who knows that after reading this article, you will find a better and more efficient way and will simplify the solution further. So, lets hold onto the faith and trust of our mind and coding fingers while we bid adieu.

Updated on: 20-Jul-2023

57 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements