|
| 1 | +#include <assert.h> |
| 2 | +#include <ctype.h> |
| 3 | +#include <limits.h> |
| 4 | +#include <math.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include <stddef.h> |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +char* readline(); |
| 13 | +char* ltrim(char*); |
| 14 | +char* rtrim(char*); |
| 15 | +char** split_string(char*); |
| 16 | + |
| 17 | +int parse_int(char*); |
| 18 | + |
| 19 | +/* |
| 20 | + * Complete the 'beautifulTriplets' function below. |
| 21 | + * |
| 22 | + * The function is expected to return an INTEGER. |
| 23 | + * The function accepts following parameters: |
| 24 | + * 1. INTEGER d |
| 25 | + * 2. INTEGER_ARRAY arr |
| 26 | + */ |
| 27 | + |
| 28 | +int beautifulTriplets(int d, int arr_count, int* arr) { |
| 29 | + int count = 0; |
| 30 | + |
| 31 | + // Iterate through the array for triplet checks |
| 32 | + for (int i = 0; i < arr_count; i++) { |
| 33 | + int first = arr[i]; |
| 34 | + int second = first + d; |
| 35 | + int third = second + d; |
| 36 | + |
| 37 | + // Check if second and third elements exist |
| 38 | + bool foundSecond = false, foundThird = false; |
| 39 | + |
| 40 | + for (int j = i + 1; j < arr_count; j++) { |
| 41 | + if (arr[j] == second) { |
| 42 | + foundSecond = true; |
| 43 | + } |
| 44 | + if (arr[j] == third && foundSecond) { |
| 45 | + foundThird = true; |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (foundSecond && foundThird) { |
| 51 | + count++; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return count; |
| 56 | +} |
| 57 | + |
| 58 | +int main() |
| 59 | +{ |
| 60 | + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); |
| 61 | + |
| 62 | + char** first_multiple_input = split_string(rtrim(readline())); |
| 63 | + |
| 64 | + int n = parse_int(*(first_multiple_input + 0)); |
| 65 | + |
| 66 | + int d = parse_int(*(first_multiple_input + 1)); |
| 67 | + |
| 68 | + char** arr_temp = split_string(rtrim(readline())); |
| 69 | + |
| 70 | + int* arr = malloc(n * sizeof(int)); |
| 71 | + |
| 72 | + for (int i = 0; i < n; i++) { |
| 73 | + int arr_item = parse_int(*(arr_temp + i)); |
| 74 | + |
| 75 | + *(arr + i) = arr_item; |
| 76 | + } |
| 77 | + |
| 78 | + int result = beautifulTriplets(d, n, arr); |
| 79 | + |
| 80 | + fprintf(fptr, "%d\n", result); |
| 81 | + |
| 82 | + fclose(fptr); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +char* readline() { |
| 88 | + size_t alloc_length = 1024; |
| 89 | + size_t data_length = 0; |
| 90 | + |
| 91 | + char* data = malloc(alloc_length); |
| 92 | + |
| 93 | + while (true) { |
| 94 | + char* cursor = data + data_length; |
| 95 | + char* line = fgets(cursor, alloc_length - data_length, stdin); |
| 96 | + |
| 97 | + if (!line) { |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + data_length += strlen(cursor); |
| 102 | + |
| 103 | + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + alloc_length <<= 1; |
| 108 | + |
| 109 | + data = realloc(data, alloc_length); |
| 110 | + |
| 111 | + if (!data) { |
| 112 | + data = '\0'; |
| 113 | + |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (data[data_length - 1] == '\n') { |
| 119 | + data[data_length - 1] = '\0'; |
| 120 | + |
| 121 | + data = realloc(data, data_length); |
| 122 | + |
| 123 | + if (!data) { |
| 124 | + data = '\0'; |
| 125 | + } |
| 126 | + } else { |
| 127 | + data = realloc(data, data_length + 1); |
| 128 | + |
| 129 | + if (!data) { |
| 130 | + data = '\0'; |
| 131 | + } else { |
| 132 | + data[data_length] = '\0'; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return data; |
| 137 | +} |
| 138 | + |
| 139 | +char* ltrim(char* str) { |
| 140 | + if (!str) { |
| 141 | + return '\0'; |
| 142 | + } |
| 143 | + |
| 144 | + if (!*str) { |
| 145 | + return str; |
| 146 | + } |
| 147 | + |
| 148 | + while (*str != '\0' && isspace(*str)) { |
| 149 | + str++; |
| 150 | + } |
| 151 | + |
| 152 | + return str; |
| 153 | +} |
| 154 | + |
| 155 | +char* rtrim(char* str) { |
| 156 | + if (!str) { |
| 157 | + return '\0'; |
| 158 | + } |
| 159 | + |
| 160 | + if (!*str) { |
| 161 | + return str; |
| 162 | + } |
| 163 | + |
| 164 | + char* end = str + strlen(str) - 1; |
| 165 | + |
| 166 | + while (end >= str && isspace(*end)) { |
| 167 | + end--; |
| 168 | + } |
| 169 | + |
| 170 | + *(end + 1) = '\0'; |
| 171 | + |
| 172 | + return str; |
| 173 | +} |
| 174 | + |
| 175 | +char** split_string(char* str) { |
| 176 | + char** splits = NULL; |
| 177 | + char* token = strtok(str, " "); |
| 178 | + |
| 179 | + int spaces = 0; |
| 180 | + |
| 181 | + while (token) { |
| 182 | + splits = realloc(splits, sizeof(char*) * ++spaces); |
| 183 | + |
| 184 | + if (!splits) { |
| 185 | + return splits; |
| 186 | + } |
| 187 | + |
| 188 | + splits[spaces - 1] = token; |
| 189 | + |
| 190 | + token = strtok(NULL, " "); |
| 191 | + } |
| 192 | + |
| 193 | + return splits; |
| 194 | +} |
| 195 | + |
| 196 | +int parse_int(char* str) { |
| 197 | + char* endptr; |
| 198 | + int value = strtol(str, &endptr, 10); |
| 199 | + |
| 200 | + if (endptr == str || *endptr != '\0') { |
| 201 | + exit(EXIT_FAILURE); |
| 202 | + } |
| 203 | + |
| 204 | + return value; |
| 205 | +} |
0 commit comments