|
| 1 | +// Link : https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup |
| 2 | + |
| 3 | +#include <assert.h> |
| 4 | +#include <limits.h> |
| 5 | +#include <math.h> |
| 6 | +#include <stdbool.h> |
| 7 | +#include <stddef.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <stdio.h> |
| 10 | +#include <stdlib.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +char* readline(); |
| 14 | +char** split_string(char*); |
| 15 | + |
| 16 | +// Complete the sockMerchant function below. |
| 17 | +int sockMerchant(int n, int ar_count, int* ar) { |
| 18 | + int freq[101]={}; |
| 19 | + int col; |
| 20 | + for(int i=0;i<n;i++) |
| 21 | + { |
| 22 | + col = *(ar+i); |
| 23 | + freq[col]++; |
| 24 | + } |
| 25 | + int pair = 0; |
| 26 | + for(int i = 0; i < 101; i++){ |
| 27 | + pair += freq[i] / 2; |
| 28 | + } |
| 29 | + return pair; |
| 30 | +} |
| 31 | + |
| 32 | +int main() |
| 33 | +{ |
| 34 | + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); |
| 35 | + |
| 36 | + char* n_endptr; |
| 37 | + char* n_str = readline(); |
| 38 | + int n = strtol(n_str, &n_endptr, 10); |
| 39 | + |
| 40 | + if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); } |
| 41 | + |
| 42 | + char** ar_temp = split_string(readline()); |
| 43 | + |
| 44 | + int* ar = malloc(n * sizeof(int)); |
| 45 | + |
| 46 | + for (int i = 0; i < n; i++) { |
| 47 | + char* ar_item_endptr; |
| 48 | + char* ar_item_str = *(ar_temp + i); |
| 49 | + int ar_item = strtol(ar_item_str, &ar_item_endptr, 10); |
| 50 | + |
| 51 | + if (ar_item_endptr == ar_item_str || *ar_item_endptr != '\0') { exit(EXIT_FAILURE); } |
| 52 | + |
| 53 | + *(ar + i) = ar_item; |
| 54 | + } |
| 55 | + |
| 56 | + int ar_count = n; |
| 57 | + |
| 58 | + int result = sockMerchant(n, ar_count, ar); |
| 59 | + |
| 60 | + fprintf(fptr, "%d\n", result); |
| 61 | + |
| 62 | + fclose(fptr); |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +char* readline() { |
| 68 | + size_t alloc_length = 1024; |
| 69 | + size_t data_length = 0; |
| 70 | + char* data = malloc(alloc_length); |
| 71 | + |
| 72 | + while (true) { |
| 73 | + char* cursor = data + data_length; |
| 74 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 75 | + |
| 76 | + if (!line) { break; } |
| 77 | + |
| 78 | + data_length += strlen(cursor); |
| 79 | + |
| 80 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; } |
| 81 | + |
| 82 | + size_t new_length = alloc_length << 1; |
| 83 | + data = realloc(data, new_length); |
| 84 | + |
| 85 | + if (!data) { break; } |
| 86 | + |
| 87 | + alloc_length = new_length; |
| 88 | + } |
| 89 | + |
| 90 | + if (data[data_length - 1] == '\n') { |
| 91 | + data[data_length - 1] = '\0'; |
| 92 | + } |
| 93 | + |
| 94 | + data = realloc(data, data_length); |
| 95 | + |
| 96 | + return data; |
| 97 | +} |
| 98 | + |
| 99 | +char** split_string(char* str) { |
| 100 | + char** splits = NULL; |
| 101 | + char* token = strtok(str, " "); |
| 102 | + |
| 103 | + int spaces = 0; |
| 104 | + |
| 105 | + while (token) { |
| 106 | + splits = realloc(splits, sizeof(char*) * ++spaces); |
| 107 | + if (!splits) { |
| 108 | + return splits; |
| 109 | + } |
| 110 | + |
| 111 | + splits[spaces - 1] = token; |
| 112 | + |
| 113 | + token = strtok(NULL, " "); |
| 114 | + } |
| 115 | + |
| 116 | + return splits; |
| 117 | +} |
0 commit comments