Hackerrank-C solutions

1D_Arrays_in_C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n=0,a=0,sum=0,i=0;
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    scanf("%d",&n);
    int* arry=(int*)malloc(n*sizeof(int));
    
    while(a<n)
    {
        scanf("%d",&arry[a]);
        sum=sum+arry[a];
        a++;
    }
    free(arry);
    printf("%d",sum);
    return 0;
}

Array-Reversal

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num, *arr, i;
    scanf("%d", &num);
    arr = (int*) malloc(num * sizeof(int));
    for(i = 0; i < num; i++) {
        scanf("%d", arr + i);
    }

    /* Write the logic to reverse the array. */

    for(i = num - 1; i >= 0; i--)
        printf("%d ", *(arr + i));
    return 0;
}
}

Bitwise-Operators

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

// Complete the following function.
void calculate_the_maximum(int n, int k) {
    int maxAnd = 0;
    int maxOr = 0;
    int maxXor = 0;

    for (int i=1; i<=n; i++) {
        for (int j=i+1; j<=n; j++) {
            if (((i&j) > maxAnd) && ((i&j) < k)) {
                maxAnd = i&j;
            }
            if (((i|j) > maxOr) && ((i|j) < k)) {
                maxOr = i|j;
            }
            if (((i^j) > maxXor) && ((i^j) < k)) {
                maxXor = i^j;
            }
        }
    }

    printf("%d\n%d\n%d\n", maxAnd, maxOr, maxXor);
}

int main() {
    int n, k;
  
    scanf("%d %d", &n, &k);
    calculate_the_maximum(n, k);
 
    return 0;
}

Calculate-the-nth-term

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

// Complete the following function.
int find_nth_term(int n, int a, int b, int c) {
    if(n == 1)
        return a;
    else if (n == 2)
        return b;
    else if (n == 3)
        return c;
    
    return find_nth_term(n-1,a,b,c)+find_nth_term(n-2,a,b,c)+find_nth_term(n-3,a,b,c);
}

int main() {
    int n, a, b, c;
  
    scanf("%d %d %d %d", &n, &a, &b, &c);
    int ans = find_nth_term(n, a, b, c);
 
    printf("%d", ans); 
    return 0;
}

Digit-Frequency

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int* nums = (int*) malloc(10 * sizeof(int));
    char c;
    
    for(int i = 0; i < 10; i++)
        *(nums+i) = 0;

    while(scanf("%c", &c) == 1)
        if(c >= '0' && c <= '9')
            (*(nums+(c-'0')))++;
    
    for(int i = 0; i < 10; i++)
        printf("%d ", *(nums+i));
    
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Dynamic-Array-in-C

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int total_number_of_shelves;
    scanf("%d", &total_number_of_shelves);
    
    int total_number_of_queries;
    scanf("%d", &total_number_of_queries);
    total_number_of_books = (int*) calloc(total_number_of_shelves, sizeof(int));
    total_number_of_pages = (int**) calloc(total_number_of_shelves, sizeof(int*));
    while (total_number_of_queries--) {
        int type_of_query;
        scanf("%d", &type_of_query);
        if (type_of_query == 1) {
            int x, y;
            scanf("%d %d", &x, &y);
            total_number_of_books[x]++;
            total_number_of_pages[x] = (int*) realloc(total_number_of_pages[x], total_number_of_books[x] * sizeof(int));
            total_number_of_pages[x][total_number_of_books[x] - 1] = y;
        }
        else if (type_of_query == 2) {
            int x, y;
            scanf("%d %d", &x, &y);
            printf("%d\n", *((total_number_of_pages + x) + y));
        }
        else {
            int x;
            scanf("%d", &x);
            printf("%d\n", *(total_number_of_books + x));
        }
    }

    for (int i = 0; i < total_number_of_shelves; i++) {
        free(total_number_of_pages[i]);
    }
    free(total_number_of_books);
    free(total_number_of_pages);

    return 0;
}

For_Loop _in_C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int a, b, i;
    scanf("%d\n%d", &a, &b);
    for(i = a; i <= b; i++) {
        if(i >= 1 && i <= 9) {
            switch(i) {
                case 1:
                    printf("one\n");
                    break;
                case 2:
                    printf("two\n");
                    break;
                case 3:
                    printf("three\n");
                    break;
                case 4:
                    printf("four\n");
                    break;
                case 5:
                    printf("five\n");
                    break;
                case 6:
                    printf("six\n");
                    break;
                case 7:
                    printf("seven\n");
                    break;
                case 8:
                    printf("eight\n");
                    break;
                case 9:
                    printf("nine\n");
                    break;
            } 
        }
        else {
            if(i % 2 == 0) {
                printf("even\n");
            }
            else {
                printf("odd\n");
            }
        }
    }
    return 0;
}

Playing with Characters

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    char c, d[50], e[100];
    scanf("%c", &c);
    scanf("\n");
    gets(d);
    gets(e);
    printf("%c\n", c);
    printf("%s\n", d);
    printf("%s", e);

    return 0;
}

Pattern Printing using loops

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int n, i, j;
    scanf("%d", &n);
    for(i = n; i >= 1; i--) {
        // First inner part of upper half
        for(j = n; j > i; j--) {
            printf("%d ", j);
        }
        
        // Second inner part of upper half
        for(j = 1; j <= (i * 2 - 1); j++) {
            printf("%d ", i);
        }
        
        // Third inner part of upper half
        for(j = i + 1; j <= n; j++) {
            printf("%d ", j);
        }
        
        printf("\n");
    }

    // Second lower half of the pattern
    for(i = 1; i < n; i++) {
        // First inner part of lower half
        for(j = n; j > i; j--) {
            printf("%d ", j);
        }
        
        // Second inner part of lower half
        for(j = 1; j <= (i * 2 - 1); j++) {
            printf("%d ", i + 1);
        }
        
        // Third inner part of lower half
        for(j = i + 1; j <= n; j++) {
            printf("%d ", j);
        }
        
        printf("\n");
    }
    return 0;
}

Post a Comment

0 Comments