Complete the code to declare the hash map for storing numbers and their indices.
int hashMap[[1]];The hash map size is set to 100000 to cover the range of possible input values efficiently.
Complete the code to initialize the hash map values to -1 indicating empty slots.
for (int i = 0; i < [1]; i++) { hashMap[i] = -1; }
The loop runs up to 100000 to initialize all hash map entries to -1.
Fix the error in the condition to check if the complement exists in the hash map.
if (hashMap[[1]] != -1) { return (int[]){hashMap[complement], i}; }
The complement is the key to check in the hash map to find if the pair exists.
Fill both blanks to store the current index in the hash map and calculate the complement.
int complement = [1] - nums[i]; hashMap[[2]] = i;
The complement is target minus current number, and we store the current index at nums[i] in the hash map.
Fill all three blanks to complete the Two Sum function returning indices of the two numbers.
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
static int hashMap[100000];
for (int i = 0; i < [1]; i++) {
hashMap[i] = -1;
}
for (int i = 0; i < numsSize; i++) {
int complement = [2] - nums[i];
if (hashMap[complement] != -1) {
int* result = malloc(2 * sizeof(int));
result[0] = hashMap[complement];
result[1] = i;
*returnSize = 2;
return result;
}
hashMap[[3]] = i;
}
*returnSize = 0;
return NULL;
}The hash map is initialized with size 100000. The complement is target - nums[i]. We store the index i at hashMap[nums[i]].
