Problem Explanation:
Given two non-negative integers represented as strings (num1
and num2
), we are tasked with calculating the product of these two numbers without converting them directly to integers (no built-in BigInteger library). We need to simulate the multiplication of two large numbers, just like how it’s done manually on paper.
The general approach is similar to multiplying numbers manually:
- Multiply each digit of the first number with each digit of the second number.
- Keep track of the positions where these products should be placed.
- Handle carries properly.
- Build the final result string.
Approach:
- Reverse the digits: We’ll iterate from the least significant digits (rightmost) of both numbers to simulate manual multiplication.
- Temporary result array: Use an array to store intermediate results, where the index represents the place value.
- Digit multiplication: Multiply each digit of
num1
with each digit ofnum2
and store the results in the appropriate positions in the temporary array. - Carry handling: Once the multiplication is complete, handle carries by processing the
tempResult
array. - Convert to string: Convert the result from the
tempResult
array to a string and handle leading zeros.
Code:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 1000
// Function to multiply two strings num1 and num2, and store the result in result
void multiplyStrings(const char* num1, const char* num2, char* result) {
int len1 = strlen(num1);
int len2 = strlen(num2);
// Create a temporary array to store the intermediate results
int tempResult[MAX_LEN] = {0};
// Multiply each digit from num1 and num2
for (int j = len2 - 1; j >= 0; --j) {
for (int i = len1 - 1; i >= 0; --i) {
int digit1 = num1[i] - '0';
int digit2 = num2[j] - '0';
int mul = digit1 * digit2;
// The position in the result array
int p1 = i + j;
int p2 = i + j + 1;
// Add the multiplication result to the tempResult at p2
int sum = mul + tempResult[p2];
tempResult[p2] = sum % 10; // Store the current digit
tempResult[p1] += sum / 10; // Carry to the next position
}
}
// Convert the result to a string, ignoring leading zeros
int index = 0;
while (index < len1 + len2 && tempResult[index] == 0) {
++index;
}
// If there are no non-zero digits, the result is "0"
if (index == len1 + len2) {
result[0] = '0';
result[1] = '';
return;
}
// Construct the result string
int k = 0;
while (index < len1 + len2) {
result[k++] = tempResult[index++] + '0';
}
result[k] = ''; // Null terminate the result string
}
int main() {
char num1[MAX_LEN] = "123"; // First number as string
char num2[MAX_LEN] = "456"; // Second number as string
char result[MAX_LEN * 2] = {0}; // Result string
multiplyStrings(num1, num2, result); // Call the function to multiply
printf("Product: %sn", result); // Print the result
return 0;
}
Explanation of the Code:
-
multiplyStrings
function:- The
tempResult
array is used to store intermediate results of the multiplication. - We iterate through the digits of
num1
andnum2
in reverse order (from least significant digit to the most significant). - Each product of two digits is stored in the
tempResult
array at the correct position. The position is determined by the sum of the indices of the digits being multiplied (i + j
andi + j + 1
for the carry). - We then handle any carries by adding them to the corresponding position in
tempResult
.
- The
-
Building the result string:
- We skip over any leading zeros in the
tempResult
array. - We then convert the result from
tempResult
into a string and store it inresult
.
- We skip over any leading zeros in the
-
Final check:
- If the result contains only zeros, we return
"0"
.
- If the result contains only zeros, we return
-
Printing the result: Finally, the main function prints the product.
Example:
-
Input:
num1 = "123"
,num2 = "456"
Process:
- Multiply each digit of
123
with456
manually, storing the intermediate results. - The intermediate array (
tempResult
) will be updated as follows:tempResult = {0, 0, 5, 5, 6, 0, 8, 8}
- The result is then built as
"56088"
.
Output:
"56088"
- Multiply each digit of
-
Input:
num1 = "2"
,num2 = "3"
Output:
"6"
Time and Space Complexity:
-
Time Complexity:
- The time complexity is O(n * m), where
n
is the length ofnum1
andm
is the length ofnum2
. This is because we perform a nested loop where each digit ofnum1
is multiplied by each digit ofnum2
.
- The time complexity is O(n * m), where
-
Space Complexity:
- The space complexity is O(n + m), where
n
andm
are the lengths ofnum1
andnum2
, respectively. This is the space required for thetempResult
array and the result string.
- The space complexity is O(n + m), where
This approach efficiently solves the problem using basic string manipulation and simulates the manual multiplication process without using any built-in BigInteger library.