HackerRank Solutions

Algorithms

Solve Me First - C

int solveMeFirst(int a, int b) {
    return a+b;
}

Simple Array Sum - C

int simpleArraySum(int ar_count, int* ar) {
     int sum = 0;
     for(int i=0;i<ar_count;i++){
         sum += ar[i];
     }
     return sum;
}

Compare the Triplets - C

int* compareTriplets(int a_count, int* a, int b_count, int* b, int* result_count) {
    *result_count =2;
    int* outArr = malloc(2 * sizeof(int));
    outArr[0]=0;
    outArr[1]=0;
    for(int i=0;i<a_count;i++){
        if(a[i]>b[i]){
            outArr[0]++;
        }
        else if(b[i]>a[i]){
            outArr[1]++;
        }
    }
    return outArr;
}

A Very Big Sum - C

long aVeryBigSum(int ar_count, long* ar) {
    long sum=0;
    for(int i=0;i<ar_count;i++){
        sum += ar[i];
    }
    return sum;
}

Diagonal Difference - C

int diagonalDifference(int arr_rows, int arr_columns, int** arr) {
    int leftDiag=0;
    int rightDiag=0;

    for(int i=0;i<arr_rows;i++){
        leftDiag+=arr[i][i];  
    }
    for(int i=0;i<arr_rows;i++){
        rightDiag+=arr[i][arr_rows-i-1];
    }
    if(leftDiag>rightDiag)
        return (leftDiag-rightDiag);
    else
        return (rightDiag-leftDiag);
}

Plus Minus - C

void plusMinus(int arr_count, int* arr) {
    int* types = malloc(3 * sizeof(int));
    for(int i=0;i<3;i++){
        types[i]=0;
    }
    for(int i = 0;i<arr_count;i++){
        if(arr[i]>0)
            types[0]++;
        else if(arr[i]<0)
            types[1]++;
        else 
            types[2]++;
    }
    for(int i=0;i<3;i++){
        printf("%.6f\n",(float)types[i]/arr_count);
    }
}

Staircase - C

void staircase(int n) {
    char str[n];
    for(int i=0;i<n;i++){
        str[i]='#';
        str[i+1]=0;
        printf("%*s",n,str); 
        if(i!=(n-1))
            printf("\n");
    }
}

Mini-Max Sum - C

void miniMaxSum(int arr_count, int* arr) {
    unsigned long long int min= LLONG_MAX;
    unsigned long long int max= 0;
    unsigned long long int sum;
    for(int miss=0;miss<arr_count;miss++){
        sum=0;
        for(int i=0;i<arr_count;i++){
            if(i!=miss)
                sum+=arr[i];
        }
        if(sum>max)
            max=sum;
        if(sum<min)
            min=sum;
    }    
    printf("%ld %ld",min,max);
}

Birthday Cake Candles - C

int birthdayCakeCandles(int ar_count, int* ar) {
    int highest=0;
    int count =0;
    for(int i =0;i<ar_count;i++){
        if(ar[i]>highest){
            highest=ar[i];
            count=1;
        }
        else if(ar[i]==highest){
            count++;
        }
    }
    return count;
}

Time Conversion - C

char* timeConversion(char* s) {
    char *str = malloc(sizeof(char)*9);

    if(s[8]=='P'){
        if((s[1]>='8')||(s[0]=='1')){
            if((s[0]=='1')&&(s[1]=='2')){
            str[0]='1';
            str[1]='2';
        }
        else{
                str[0]='2';
                str[1]=((((((int)(s[1])-48))+2)%10)+48);
        }   
        }
        else{
            str[0]='1';
            str[1]=((((((int)(s[1])-48))+2)%10)+48);
        }
    }
    else{
        if((s[0]=='1')&&(s[1]=='2')){
            str[0]='0';
            str[1]='0';
        }
        else{
        str[0]=s[0];
        str[1]=s[1];
        }     
    }
    for(int i=2;i<8;i++){
        str[i]=s[i];
    }
    str[8]=0;
    return str;
}

Grading Students - C

int* gradingStudents(int grades_count, int* grades, int* result_count) {
    *result_count=grades_count;
    int *o = malloc(grades_count * sizeof(int));
    for(int i=0;i<grades_count;i++){
        if(grades[i]<38){
            o[i]=grades[i];
        }
        else if((grades[i]%5)>2){
            o[i]=grades[i]+(5-(grades[i]%5));
        }
        else{
            o[i]=grades[i];
        }
    }
    return o;
}

Apple and Orange - C

void countApplesAndOranges(int s, int t, int a, int b, int apples_count, int* apples, int oranges_count, int* oranges) {
    int fruitIn =0;
    for(int i=0;i<apples_count;i++){
        if(((apples[i]+a)>=s)&&((apples[i]+a)<=t))
            fruitIn++;
    }
    printf("%d\n",fruitIn);
    fruitIn =0;
    for(int i=0;i<oranges_count;i++){
        if(((oranges[i]+b)>=s)&&((oranges[i]+b)<=t))
            fruitIn++;
    }
    printf("%d",fruitIn);
}

Kangaroo - C

char* kangaroo(int x1, int v1, int x2, int v2) {
    int i=0;
    while((v1*i+x1)>=(v2*i+x2)){
        if((v1*i+x1)==(v2*i+x2))
            return "YES";
        i++; 
    }
    while((v1*i+x1)<=(v2*i+x2)){
        if((v1*i+x1)==(v2*i+x2))
            return "YES";
        i++; 
    }
    return "NO";
}