text
stringlengths 0
82
|
---|
// Index to store the position of |
// the first non-zero digit |
int i, len = strlen(str); |
for (i = 0; i < len - 1; i++) , |
if (str*i+ != '0') |
break; |
- |
// Shift all non-zero digits to the |
// beginning of the string |
for (int j = 0; j < len - i; j++) |
str*j+ = str*i + j+; |
// Null terminate the string |
str*len - i+ = '\0'; |
return str; |
- |
// Function to multiply two numbers |
// using Karatsuba algorithm |
char* multiply(const char* A, const char* B) |
, |
const char* str1 = A; |
const char* str2 = B; |
if (strlen(A) > strlen(B)) , |
str1 = B; |
str2 = A; |
- |
// Make both numbers to have |
// same digits |
int n1 = strlen(str1), n2 = strlen(str2); |
while (n2 > n1) , |
n1++; |
- |
// Base case |
if (n1 == 1) , |
// If the length of strings is 1, |
// then return their product |
int ans = atoi(str1) * atoi(str2); |
sprintf(str1, "%d", ans); |
return str1; |
- |
// Add zeros in the beginning of |
// the strings when length is odd |
if (n1 % 2 == 1) , |
n1++; |
char temp*1000+; |
strcpy(temp, str1); |
strcpy(str1, "0"); |
strcat(str1, temp); |
strcpy(temp, str2); |
strcpy(str2, "0"); |
strcat(str2, temp); |
- |
char Al*1000+, Ar*1000+, Bl*1000+, Br*1000+; |
// Find the values of Al, Ar, |
// Bl, and Br. |
for (int i = 0; i < n1 / 2; ++i) , |
Al*i+ = str1*i+; |
Bl*i+ = str2*i+; |
Ar*i+ = str1*n1 / 2 + i+; |
Br*i+ = str2*n1 / 2 + i+; |
- |
Al*n1 / 2+ = '\0'; |
Ar*n1 / 2+ = '\0'; |
Bl*n1 / 2+ = '\0'; |
Br*n1 / 2+ = '\0'; |
// Recursively call the function |
// to compute smaller product |
// Stores the value of Al * Bl |
char* p = multiply(Al, Bl); |
// Stores the value of Ar * Br |
char* q = multiply(Ar, Br); |
// Stores value of ((Al + Ar)*(Bl + Br) |
// - Al*Bl - Ar*Br) |
char* r = findDiff( |
findSum(Al, Ar), |
findSum(Bl, Br)); |
// Multiply p by 10^n |
for (int i = 0; i < n1; ++i) |
strcat(p, "0"); |
// Multiply s by 10^(n/2) |
for (int i = 0; i < n1 / 2; ++i) |
strcat(r, "0"); |
// Calculate final answer p + r + s |
char* ans = findSum(p, findSum(q, r)); |
Subset Sum |
#include <stdio.h> |
#include <limits.h> |
#include <stdbool.h> |
bool foundSolution = false; |
int weights[10]; |
int solution[10]; |
int n; |
int m; |
void subsetSum(int currentSum, int index) |
{ |
if(index == n) { |
if(currentSum == m) { |
foundSolution = true; |
for(int i=0; i<n; i++) |
printf("%d ", solution[i]); |
printf("\n"); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.