给定一个数字N和一个数字K (1 < K <= 9),任务是找到两个整数A和B ,使得A + B = N并且数字 K不存在于 A 或 B 中。
例子:
Input: N = 443, K = 4
Output: 256, 187
Explanation:
Sum = 256 + 187 = 443 = N
Also, K(4) is not present in 256 or 187
Hence, 256 and 187 are valid output.
Input: N = 678, K = 9
Output: 311, 367
Explanation:
Sum = 311 + 367 = 678 = N
Also, K(9) is not present in 311 or 367
Hence, 311 and 367 are valid output.
天真的方法:
一个简单的方法是运行一个循环并考虑从 1 到 N-1 的每个元素(比如 A)。由于 A 和 B 的总和必须是 N,因此对应的 B 将是 (N – A)。现在检查 A 和 B 是否都包含 K,如果不包含,则打印 A 和 B。对于较大的输入,如 N = 10 18 ,这种方法将失败。
时间复杂度: O(N)
辅助空间: O(1)
有效的方法:
可以更有效地解决给定的问题。这个想法是将 N 的每个数字D分成两部分D1和D2 ,使得 D1 + D2 = D。取两个字符串A和B ,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 以确保 D1 + D2 = D 对于偶数和奇数 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
Javascript
11671, 22002
时间复杂度: O(M)
辅助空间: O(M)
其中,M 是 N 中的位数。