Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added submissions/AnshumanRai/AddString/Question2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions submissions/AnshumanRai/AddString/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
Binary file added submissions/AnshumanRai/AddString/solution.exe
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions submissions/AnshumanRai/TwoSumArray/solution.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>

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;
}
Binary file added submissions/AnshumanRai/TwoSumArray/solution.exe
Binary file not shown.