"Hello World!" in C

Sort by

recency

|

656 Discussions

|

  • + 0 comments

    include

    int main() {

    printf("hello, world! \n"); printf("welcome to c programming");

    }

  • + 0 comments

    //road repair question

    /* 2. Road Repair | A number of points along the highway are in need of | repair. An equal number of crews are available, stationed | at various points along the highway. They must move | along the highway to reach an assigned point. Given that | one crew must be assigned to each job, what is the | minimum total amount of distance traveled by all crews | before they can begin work? | For example, given crews at points {1,3,5} and required | repairs at {3,5,7}, one possible minimum assignment | would be {1— 3,3 — 5, 5 — 7} for a total of 6 units | traveled. | Function Description | Complete the function getMinCost in the editor below. The function should return the minimum possible total | distance traveled as an integer. | getMinCost has the following parameter(s): |

    crewld[crewld[0],...crewld[n-1]] : a vector of integers |

    jobld[jobld[0]....jobld[n-1]] : a vector of integers |

    Constraints « 1

    Explanation | By index, crewld[i] — jobld[i], { (0 — 0), (1 —2),(2—4), | (3—3),(4—1)}is one possible assignment for a | minimum cost of 17. Showing element values, this is { (5 | —9,B8-3),(1-1,(4—15),(6—8)}yieldinga | total travel distance of 4+ 0+ 0+ 11 +2=17. | v Sample Case 1 | Sample Input For Custom Testing |

    10 | Sample Output |

    18 | Explanation | By index, {(1—0),(0—1),(2—2),(3—3)}isone | possible assignment for a minimum cost of 18. | */

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    char* readline(); char* ltrim(char*); char* rtrim(char*); int parse_int(char*);

    // Comparison function for qsort int compare(const void* a, const void* b) { return ((int)a - (int)b); }

    long getMinCost(int crew_id_count, int* crew_id, int job_id_count, int* job_id) { // Debug: Print input sizes fprintf(stderr, "crew_id_count: %d, job_id_count: %d\n", crew_id_count, job_id_count);

    // Check if counts match (should always be true per problem constraints)
    if (crew_id_count != job_id_count) {
        fprintf(stderr, "Error: Mismatched crew and job counts\n");
        return -1; // Return invalid value for debugging
    }
    
    int n = crew_id_count;
    
    // Debug: Print crew_id before sorting
    fprintf(stderr, "crew_id before sorting: ");
    for (int i = 0; i < n; i++) {
        fprintf(stderr, "%d ", crew_id[i]);
    }
    fprintf(stderr, "\n");
    
    // Debug: Print job_id before sorting
    fprintf(stderr, "job_id before sorting: ");
    for (int i = 0; i < n; i++) {
        fprintf(stderr, "%d ", job_id[i]);
    }
    fprintf(stderr, "\n");
    
    // Sort crew_id array
    qsort(crew_id, n, sizeof(int), compare);
    
    // Sort job_id array
    qsort(job_id, n, sizeof(int), compare);
    
    // Debug: Print crew_id after sorting
    fprintf(stderr, "crew_id after sorting: ");
    for (int i = 0; i < n; i++) {
        fprintf(stderr, "%d ", crew_id[i]);
    }
    fprintf(stderr, "\n");
    
    // Debug: Print job_id after sorting
    fprintf(stderr, "job_id after sorting: ");
    for (int i = 0; i < n; i++) {
        fprintf(stderr, "%d ", job_id[i]);
    }
    fprintf(stderr, "\n");
    
    // Calculate total distance
    long total_distance = 0;
    for (int i = 0; i < n; i++) {
        long distance = labs((long)crew_id[i] - (long)job_id[i]);
        total_distance += distance;
        // Debug: Print each distance
        fprintf(stderr, "Distance for crew %d to job %d: %ld\n", crew_id[i], job_id[i], distance);
    }
    
    // Debug: Print total distance
    fprintf(stderr, "Total distance: %ld\n", total_distance);
    
    return total_distance;
    

    }

    int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    int crew_id_count = parse_int(ltrim(rtrim(readline())));
    
    int* crew_id = malloc(crew_id_count * sizeof(int));
    
    for (int i = 0; i < crew_id_count; i++) {
        int crew_id_item = parse_int(ltrim(rtrim(readline())));
    
        *(crew_id + i) = crew_id_item;
    }
    
    int job_id_count = parse_int(ltrim(rtrim(readline())));
    
    int* job_id = malloc(job_id_count * sizeof(int));
    
    for (int i = 0; i < job_id_count; i++) {
        int job_id_item = parse_int(ltrim(rtrim(readline())));
    
        *(job_id + i) = job_id_item;
    }
    
    long result = getMinCost(crew_id_count, crew_id, job_id_count, job_id);
    
    fprintf(fptr, "%ld\n", result);
    
    fclose(fptr);
    
    return 0;
    

    }

    char* readline() { size_t alloc_length = 1024; size_t data_length = 0;

    char* data = malloc(alloc_length);
    
    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
    
        if (!line) {
            break;
        }
    
        data_length += strlen(cursor);
    
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }
    
        alloc_length <<= 1;
    
        data = realloc(data, alloc_length);
    
        if (!data) {
            data = '\0';
    
            break;
        }
    }
    
    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    
        data = realloc(data, data_length);
    
        if (!data) {
            data = '\0';
        }
    } else {
        data = realloc(data, data_length + 1);
    
        if (!data) {
            data = '\0';
        } else {
            data[data_length] = '\0';
        }
    }
    
    return data;
    

    }

    char* ltrim(char* str) { if (!str) { return '\0'; }

    if (!*str) {
        return str;
    }
    
    while (*str != '\0' && isspace(*str)) {
        str++;
    }
    
    return str;
    

    }

    char* rtrim(char* str) { if (!str) { return '\0'; }

    if (!*str) {
        return str;
    }
    
    char* end = str + strlen(str) - 1;
    
    while (end >= str && isspace(*end)) {
        end--;
    }
    
    *(end + 1) = '\0';
    
    return str;
    

    }

    int parse_int(char* str) { char* endptr; int value = strtol(str, &endptr, 10);

    if (endptr == str || *endptr != '\0') {
        exit(EXIT_FAILURE);
    }
    
    return value;
    

    }

  • + 0 comments

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    define ALPHABET_SIZE 26

    define MAX_SIGNATURE_LEN 100

    // Node structure for our hashmap typedef struct Node { char* key; int count; struct Node* next; } Node;

    define TABLE_SIZE 10007

    Node* hash_table[TABLE_SIZE];

    // Hash function for the signature key unsigned int hash(const char* key) { unsigned int h = 0; while (*key) { h = (h * 31 + *key++) % TABLE_SIZE; } return h; }

    // Inserts or updates count in the hashmap void insert(const char* key) { unsigned int h = hash(key); Node* curr = hash_table[h];

    while (curr) {
        if (strcmp(curr->key, key) == 0) {
            curr->count++;
            return;
        }
        curr = curr->next;
    }
    
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->key = strdup(key);
    new_node->count = 1;
    new_node->next = hash_table[h];
    hash_table[h] = new_node;
    

    }

    // Gets count from hashmap int get(const char* key) { unsigned int h = hash(key); Node* curr = hash_table[h];

    while (curr) {
        if (strcmp(curr->key, key) == 0) {
            return curr->count;
        }
        curr = curr->next;
    }
    return 0;
    

    }

    // Creates a frequency signature from a word void get_signature(const char* word, char* sig) { int freq[ALPHABET_SIZE] = {0}; for (int i = 0; word[i]; i++) { freq[word[i] - 'a']++; } int pos = 0; for (int i = 0; i < ALPHABET_SIZE; i++) { if (freq[i] > 0) { pos += sprintf(sig + pos, "%c%d", 'a' + i, freq[i]); } } }

    // Main function to solve the problem int* stringAnagram(int dictionary_count, char** dictionary, int query_count, char** query, int* result_count) { result_count = query_count; int result = (int*)calloc(query_count, sizeof(int));

    // Build the hashmap using frequency signatures
    for (int i = 0; i < dictionary_count; i++) {
        char sig[MAX_SIGNATURE_LEN] = {0};
        get_signature(dictionary[i], sig);
        insert(sig);
    }
    
    // For each query, look up the count using the signature
    for (int i = 0; i < query_count; i++) {
        char sig[MAX_SIGNATURE_LEN] = {0};
        get_signature(query[i], sig);
        result[i] = get(sig);
    }
    
    return result;
    

    }

    /* Remaining boilerplate (same as HackerRank's default) */

    include

    char* readline() { size_t alloc_length = 1024; size_t data_length = 0; char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
        if (!line) break;
    
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') break;
    
        alloc_length <<= 1;
        data = realloc(data, alloc_length);
        if (!data) break;
    }
    
    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
        data = realloc(data, data_length);
    } else {
        data = realloc(data, data_length + 1);
        data[data_length] = '\0';
    }
    
    return data;
    

    }

    char* ltrim(char* str) { while (isspace(*str)) str++; return str; }

    char* rtrim(char* str) { char* end = str + strlen(str) - 1; while (end >= str && isspace(*end)) end--; *(end + 1) = '\0'; return str; }

    int parse_int(char* str) { return strtol(str, NULL, 10); }

    int main() { FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    int dictionary_count = parse_int(ltrim(rtrim(readline())));
    char** dictionary = malloc(dictionary_count * sizeof(char*));
    for (int i = 0; i < dictionary_count; i++) {
        dictionary[i] = readline();
    }
    
    int query_count = parse_int(ltrim(rtrim(readline())));
    char** query = malloc(query_count * sizeof(char*));
    for (int i = 0; i < query_count; i++) {
        query[i] = readline();
    }
    
    int result_count;
    int* result = stringAnagram(dictionary_count, dictionary, query_count, query, &result_count);
    
    for (int i = 0; i < result_count; i++) {
        fprintf(fptr, "%d", result[i]);
        if (i != result_count - 1) fprintf(fptr, "\n");
    }
    fprintf(fptr, "\n");
    
    fclose(fptr);
    return 0;
    

    }

  • + 0 comments

    include

    include

    long gcd(long a, long b) { while (b != 0) { long temp = b; b = a % b; a = temp; } return a; }

    typedef struct { long num; long den; } Ratio;

    int compare(const void* a, const void* b) { Ratio* r1 = (Ratio*)a; Ratio* r2 = (Ratio*)b; if (r1->num == r2->num) { if (r1->den == r2->den) return 0; return (r1->den > r2->den) ? 1 : -1; } return (r1->num > r2->num) ? 1 : -1; }

    long nearlySimilarRectangles(int n, long sides[][2]) { if (n <= 1) return 0;

    Ratio* ratios = malloc(n * sizeof(Ratio));
    if (!ratios) {
        printf("Memory allocation failed\n");
        return -1;
    }
    
    for (int i = 0; i < n; i++) {
        long a = sides[i][0];
        long b = sides[i][1];
        long g = gcd(a, b);
        ratios[i].num = a / g;
        ratios[i].den = b / g;
    }
    
    qsort(ratios, n, sizeof(Ratio), compare);
    
    long count = 0;
    long group = 1;
    
    for (int i = 1; i < n; i++) {
        if (ratios[i].num == ratios[i - 1].num &&
            ratios[i].den == ratios[i - 1].den) {
            group++;
        } else {
            count += (group * (group - 1)) / 2;
            group = 1;
        }
    }
    count += (group * (group - 1)) / 2;
    
    free(ratios);
    return count;
    

    }

    int main() { int n; int m; // not used scanf("%d", &n); scanf("%d", &m); // discard this as per input format

    long sides[n][2];
    
    for (int i = 0; i < n; i++) {
        scanf("%ld %ld", &sides[i][0], &sides[i][1]);
    }
    
    long result = nearlySimilarRectangles(n, sides);
    printf("%ld\n", result);
    return 0;
    

    }

  • + 0 comments

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    char* readline(); char* ltrim(char*); char* rtrim(char*); int parse_int(char*);

    // Comparison function for qsort int compare(const void *a, const void b) { return ((int*)a - (int)b); }

    /* * Complete the 'filledOrders' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER_ARRAY order * 2. INTEGER k */

    int filledOrders(int order_count, int* order, int k) { qsort(order, order_count, sizeof(int), compare); // Sort the orders int count = 0; for (int i = 0; i < order_count; i++) { if (order[i] <= k) { k -= order[i]; count++; } else { break; } } return count; }

    int main() { int order_count = parse_int(ltrim(rtrim(readline())));

    int* order = malloc(order_count * sizeof(int));
    
    for (int i = 0; i < order_count; i++) {
        int order_item = parse_int(ltrim(rtrim(readline())));
        order[i] = order_item;
    }
    
    int k = parse_int(ltrim(rtrim(readline())));
    
    int result = filledOrders(order_count, order, k);
    
    printf("%d\n", result);
    
    free(order);
    
    return 0;
    

    }

    // Helper functions for input parsing

    char* readline() { size_t alloc_length = 1024; size_t data_length = 0;

    char* data = malloc(alloc_length);
    if (!data) return NULL;
    
    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
        if (!line) break;
    
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') break;
    
        alloc_length *= 2;
        data = realloc(data, alloc_length);
        if (!data) return NULL;
    }
    
    if (data[data_length - 1] == '\n') data[data_length - 1] = '\0';
    
    return data;
    

    }

    char* ltrim(char* str) { while (isspace(*str)) str++; return str; }

    char* rtrim(char* str) { if (*str == '\0') return str;

    char* end = str + strlen(str) - 1;
    while (end > str && isspace(*end)) end--;
    
    *(end + 1) = '\0';
    return str;
    

    }

    int parse_int(char* str) { return (int)strtol(str, NULL, 10); }