diff --git a/submissions/AnshumanRai/AddString/Question2.png b/submissions/AnshumanRai/AddString/Question2.png new file mode 100644 index 0000000..4f75b5d Binary files /dev/null and b/submissions/AnshumanRai/AddString/Question2.png differ diff --git a/submissions/AnshumanRai/AddString/solution.c b/submissions/AnshumanRai/AddString/solution.c new file mode 100644 index 0000000..06390d8 --- /dev/null +++ b/submissions/AnshumanRai/AddString/solution.c @@ -0,0 +1,43 @@ +#include +#include +#include + +void reverse(char* str) { + int len = strlen(str); + for (int i = 0; i < len / 2; i++) { + char tmp = str[i]; + str[i] = str[len - 1 - i]; + str[len - 1 - i] = tmp; + } +} + +char* addStrings(char* num1, char* num2) { + int len1 = strlen(num1); + int len2 = strlen(num2); + int maxLen = (len1 > len2 ? len1 : len2) + 2; + char* result = (char*)malloc(maxLen); + int i = len1 - 1, j = len2 - 1, k = 0, carry = 0; + + while (i >= 0 || j >= 0 || carry) { + int digit1 = (i >= 0) ? num1[i--] - '0' : 0; + int digit2 = (j >= 0) ? num2[j--] - '0' : 0; + int sum = digit1 + digit2 + carry; + result[k++] = (sum % 10) + '0'; + carry = sum / 10; + } + + result[k] = '\0'; + reverse(result); + return result; +} + +int main() { + char num1[] = "456"; + char num2[] = "77"; + + char* sum = addStrings(num1, num2); + printf("Sum: %s\n", sum); + + free(sum); + return 0; +} diff --git a/submissions/AnshumanRai/AddString/solution.exe b/submissions/AnshumanRai/AddString/solution.exe new file mode 100644 index 0000000..5fe5564 Binary files /dev/null and b/submissions/AnshumanRai/AddString/solution.exe differ diff --git a/submissions/AnshumanRai/TwoSumArray/Question1.png b/submissions/AnshumanRai/TwoSumArray/Question1.png new file mode 100644 index 0000000..162f201 Binary files /dev/null and b/submissions/AnshumanRai/TwoSumArray/Question1.png differ diff --git a/submissions/AnshumanRai/TwoSumArray/solution.c b/submissions/AnshumanRai/TwoSumArray/solution.c new file mode 100644 index 0000000..5cecfb6 --- /dev/null +++ b/submissions/AnshumanRai/TwoSumArray/solution.c @@ -0,0 +1,71 @@ +/* Two Sum +Solved +Easy +Topics +premium lock icon +Companies +Hint +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +You may assume that each input would have exactly one solution, and you may not use the same element twice. + +You can return the answer in any order. + + + +Example 1: + +Input: nums = [2,7,11,15], target = 9 +Output: [0,1] +Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. +Example 2: + +Input: nums = [3,2,4], target = 6 +Output: [1,2] +Example 3: + +Input: nums = [3,3], target = 6 +Output: [0,1] + + +Constraints: + +2 <= nums.length <= 104 +-109 <= nums[i] <= 109 +-109 <= target <= 109 +Only one valid answer exists.*/ +#include +#include + +int* twoSum(int* nums, int numsSize, int target, int* returnSize) { + for (int i = 0; i < numsSize; i++) { + for (int j = i + 1; j < numsSize; j++) { + if (nums[i] + nums[j] == target) { + int* result = (int*)malloc(2 * sizeof(int)); + result[0] = i; + result[1] = j; + *returnSize = 2; + return result; + } + } + } + *returnSize = 0; + return NULL; +} + +int main() { + int nums[] = {2, 7, 11, 15}; + int target = 9; + int returnSize; + + int* result = twoSum(nums, 4, target, &returnSize); + + if (returnSize == 2) { + printf("Output: [%d, %d]\n", result[0], result[1]); + free(result); // Free allocated memory + } else { + printf("No valid pair found.\n"); + } + + return 0; +} diff --git a/submissions/AnshumanRai/TwoSumArray/solution.exe b/submissions/AnshumanRai/TwoSumArray/solution.exe new file mode 100644 index 0000000..3a94e62 Binary files /dev/null and b/submissions/AnshumanRai/TwoSumArray/solution.exe differ