📜  找到两个总和为N的数字,使它们都不包含数字K

📅  最后修改于: 2021-04-24 04:19:43             🧑  作者: Mango

给定数字N和数字K (1 A和B ,使得A + B = N并且数字K不存在于A或B中。

例子:

天真的方法:
一种简单的方法是运行一个循环,并考虑从1到N-1的每个元素(例如A)。由于A和B的总和必须为N,因此对应的B为(N – A)。现在检查A和B是否都包含K,如果不包含K,则打印A和B。对于较大的输入(如N = 10 18),此方法将失败。

时间复杂度: O(N)
辅助空间: O(1)

高效方法:
给定的问题可以更有效地解决。想法是将N的每个数字D分为两个部分D1D2 ,使得D1 + D2 =D。取两个字符串AB ,A将包含所有D1,而字符串B将包含所有D2。
例如,N = 4467和K = 4

D    D1    D2
4    2    2
4    2    2
6    6    0
7    7    0    

在这里,A将为2267,而B将为2200。
此方法的详细步骤如下:
1.遍历N并考虑其每个数字D。
2.如果数字D与K相同,则将其分为D1 = D / 2和D2 = D / 2 + D%2。
D%2被添加到D2以确保偶数和奇数D的D1 + D2 =D。
3.否则,将其分为D1 = D和D2 = 0。
4.继续将D1附加到字符串A,并将D2附加到字符串B。
5.最后,在删除前导零(如果有)之后,打印字符串A和B。

下面是上述方法的实现:

C++
// C++ implementation of
// the above approach
 
#include 
using namespace std;
 
string removeLeadingZeros(string str)
{
    // Count leading zeros
    int i = 0;
    int n = str.length();
    while (str[i] == '0' && i < n)
        i++;
 
    // It removes i characters
    // starting from index 0
    str.erase(0, i);
 
    return str;
}
 
void findPairs(int sum, int K)
{
 
    string A, B;
    A = "";
    B = "";
 
    string N = to_string(sum);
    int n = N.length();
 
    // Check each digit of the N
    for (int i = 0; i < n; i++) {
 
        int D = N[i] - '0';
 
        // If digit is K break it
        if (D == K) {
            int D1, D2;
            D1 = D / 2;
 
            // For odd numbers
            D2 = D / 2 + D % 2;
 
            // Add D1 to A and D2 to B
            A = A + char(D1 + '0');
            B = B + char(D2 + '0');
        }
 
        // If the digit is not K,
        // no need to break
        // string D in A and 0 in B
        else {
            A = A + char(D + '0');
            B = B + '0';
        }
    }
 
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
 
    // Print the answer
    cout << A << ", " << B << endl;
}
 
// Driver code
int main()
 
{
    int N = 33673;
    int K = 3;
 
    findPairs(N, K);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
static String removeLeadingZeros(String str)
{
     
    // Count leading zeros
    int i = 0;
    int n = str.length();
     
    while (str.charAt(i) == '0' && i < n)
        i++;
 
    // It removes i characters
    // starting from index 0
    str = str.substring(i);
 
    return str;
}
 
static void findPairs(int sum, int K)
{
 
    String A, B;
    A = "";
    B = "";
 
    String N = String.valueOf(sum);
    int n = N.length();
 
    // Check each digit of the N
    for(int i = 0; i < n; i++)
    {
       int D = N.charAt(i) - '0';
        
       // If digit is K break it
       if (D == K)
       {
           int D1, D2;
           D1 = D / 2;
            
           // For odd numbers
           D2 = D / 2 + D % 2;
            
           // Add D1 to A and D2 to B
           A = A + (char)(D1 + '0');
           B = B + (char)(D2 + '0');
       }
        
       // If the digit is not K,
       // no need to break
       // String D in A and 0 in B
       else
       {
           A = A + (char)(D + '0');
           B = B + '0';
       }
    }
     
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
 
    // Print the answer
    System.out.print(A + ", " + B + "\n");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 33673;
    int K = 3;
 
    findPairs(N, K);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of the
# above approach
def removeLeadingZeros(Str):
     
    # Count leading zeros
    i = 0
    n = len(Str)
       
    while (Str[i] == '0' and i < n):
        i += 1
   
    # It removes i characters
    # starting from index 0
    Str = Str[i:]
    return Str
   
def findPairs(Sum, K):
  
    A = ""
    B = ""
   
    N = str(Sum)
    n = len(N)
 
    # Check each digit of the N
    for i in range(n):
     
       D = int(N[i]) - int('0')
          
       # If digit is K break it
       if (D == K):
           D1 = D // 2;
              
           # For odd numbers
           D2 = (D // 2) + (D % 2)
 
           # Add D1 to A and D2 to B
           A = A + (str)(D1 + int('0'))
           B = B + (str)(D2 + int('0'))
            
       # If the digit is not K,
       # no need to break
       # String D in A and 0 in B
       else:
           A = A + (str)(D + int('0'))
           B = B + '0'
     
    # Remove leading zeros
    A = removeLeadingZeros(A)
    B = removeLeadingZeros(B)
   
    # Print the answer
    print(A + ", " + B)
   
# Driver code
N = 33673
K = 3
 
findPairs(N, K)
 
# This code is contributed divyeshrabadiya07


C#
// C# implementation of the above approach
using System;
 
class GFG{
  
static String removeLeadingZeros(String str)
{
      
    // Count leading zeros
    int i = 0;
    int n = str.Length;
      
    while (str[i] == '0' && i < n)
        i++;
  
    // It removes i characters
    // starting from index 0
    str = str.Substring(i);
  
    return str;
}
  
static void findPairs(int sum, int K)
{
  
    String A, B;
    A = "";
    B = "";
  
    String N = String.Join("", sum);
    int n = N.Length;
  
    // Check each digit of the N
    for(int i = 0; i < n; i++)
    {
       int D = N[i] - '0';
         
       // If digit is K break it
       if (D == K)
       {
           int D1, D2;
           D1 = D / 2;
             
           // For odd numbers
           D2 = D / 2 + D % 2;
             
           // Add D1 to A and D2 to B
           A = A + (char)(D1 + '0');
           B = B + (char)(D2 + '0');
       }
         
       // If the digit is not K,
       // no need to break
       // String D in A and 0 in B
       else
       {
           A = A + (char)(D + '0');
           B = B + '0';
       }
    }
      
    // Remove leading zeros
    A = removeLeadingZeros(A);
    B = removeLeadingZeros(B);
  
    // Print the answer
    Console.Write(A + ", " + B + "\n");
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 33673;
    int K = 3;
  
    findPairs(N, K);
}
}
 
// This code is contributed by sapnasingh4991


输出:
11671, 22002

时间复杂度: O(M)
辅助空间: O(M)
在此,M是N中的位数。