-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem54.c
More file actions
91 lines (76 loc) · 1.88 KB
/
problem54.c
File metadata and controls
91 lines (76 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 100000
// Memory checker for safer malloc/realloc/calloc
int memoryCheck(void *ptr)
{
if (!ptr)
{
printf("Memory allocation failed. Exiting...\n");
return 1;
}
return 0;
}
// Reads space-separated integers dynamically
int *readInput(int *length)
{
char input[BUFFER_SIZE];
int capacity = 10, count = 0;
int *arr = (int *)malloc(capacity * sizeof(int));
if (memoryCheck(arr))
return NULL;
printf("Enter the enchantment codes (space-separated): ");
fgets(input, BUFFER_SIZE, stdin);
char *token = strtok(input, " \n");
while (token)
{
int val = atoi(token);
if (count >= capacity)
{
capacity *= 2;
arr = (int *)realloc(arr, capacity * sizeof(int));
if (memoryCheck(arr))
return NULL;
}
arr[count++] = val;
token = strtok(NULL, " \n");
}
*length = count;
return arr;
}
// Finds the duplicate and missing number from the enchantment list
void findDuplicateAndMissing(int *nums, int n, int *duplicate, int *missing)
{
int *freq = (int *)calloc(n + 1, sizeof(int));
if (memoryCheck(freq))
return;
for (int i = 0; i < n; i++)
{
freq[nums[i]]++;
}
for (int i = 1; i <= n; i++)
{
if (freq[i] == 0)
*missing = i;
else if (freq[i] == 2)
*duplicate = i;
}
free(freq);
}
// Displays the final result
void displayResult(int duplicate, int missing){
printf("\n[%d, %d]\n", duplicate, missing);
}
int main()
{
int *nums = NULL, n = 0;
int duplicate = -1, missing = -1;
nums = readInput(&n);
if (!nums)
return 1;
findDuplicateAndMissing(nums, n, &duplicate, &missing);
displayResult(duplicate, missing);
free(nums);
return 0;
}