HackerRank

John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

For example, there are n = 7 socks with colours ar = [1,2,1,2,1,3,2]. There is one pair of colour 1 and one of colour 2. There are three odd socks left, one of each colour. The number of pairs is 2.

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

int main() {

   int i,t,c,j,a[101];
    scanf("%d",&t);
    for(i=0;i<t;i++){
        scanf("%d",&a[i]);
    }
    for(i=0;i<t;i++){
        for(j=i+1;j<t;j++){
            if(a[i]==a[j] && a[i]>0){
                c++;
                a[i]=0;
                a[j]=0;
            }
        }
    }
    printf("%d",c);
    return 0;
}

OR


#include<stdio.h>
int main(){
int n,i,j,p=0,sum=0;
scanf(“%d”,&n);
int a[n],b[100];
for(i=0;i<n;i++)
    scanf(“%d”,&a[i]);
    for(j=1;j<=100;j++){
        for(i=0;i<n;i++)
            if(j==a[i]){
                p++;
                b[j]=p;
                p=0;
            }
            }
            for(i=1;i<=100;i++){
                j=b[i]/2;
                sum +=j;
            }

 

printf(“%d”,sum);
 return 0;
}

Diagonal Difference

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

For example, the square matrix  is shown below:

1 2 3
4 5 6
9 8 9  

The left-to-right diagonal = . The right to left diagonal = . Their absolute difference is .

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 

int main()
{
int i,j,n ,a[100][100];
int d1=0,d2=0;
   scanf(“%d”,&n);
   for(i=0;i<n;i++)
       for(j=0;j<n;j++)
       scanf(“%d”,&a[i][j]);
   for(i=0;i<n;i++){
       d1+=a[i][i];
       d2+=a[i][n-1-i];
   }
   int m=abs(d1-d2);
   printf(“%d”,m);
    return 0;
}

Mini-Max Sum

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

#include <stdio.h>
int main()
{
  long long int a[5],i,max=-1,min=100000000000,sum=0;
    for(i=0;i<5;i++){
        scanf(“%lld”,&a[i]);
        sum = sum + a[i];
        if(a[i]<min){
            min = a[i];
        }
        if(a[i]>max){
            max = a[i];
        }
    }
    printf(“%lld %lld”,sum-max,sum-min);
    return 0;
}

Birthday Cake Candles

You are in charge of the cake for a child’s birthday. You have decided the cake will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles. Count how many candles are tallest.

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
   int i,n, max = 0;
scanf(“%d”, & n);
int a[n];
int count = 0;
for (i = 1; i <= n; i++) {
scanf(“%d”, & a[i]);
if (max < a[i])
max = a[i];
}
for (i = 1; i <= n; i++)
if (a[i] == max)
count++;
printf(“%d”, count);
    return 0;
}

Kangaroo – HackerRank Solution in C

You are choreographing a circus show with various animals. For one act, you are given two kangaroos on a number line ready to jump in the positive direction (i.e, toward positive infinity).

  • The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump.
  • The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump.
#include <math.h>
#include <stdio.h>
int main()
{
    int x1,v1,x2,v2,temp; 
    scanf("%d %d %d %d",&x1,&v1,&x2,&v2);
    if(x1>x2)
    {
        temp=x1;
        x1=x2;
        x2=temp;
        temp=v1;
        v1=v2;
        v2=temp;
    }
    while(x1<x2)
    {
        x1+=v1;
        x2+=v2;
    }
    if(x1==x2)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

Plus Minus – HackerRank Solution in C

#include <stdio.h>

 

int main(){
    int n, a[100],i;
    float f1=0, f2=0, f3=0;
    scanf(“%d”,&n);
    for(i=0;i<n;i++){
       scanf(“%d”,&a[i]);
       if(a[i]>0){
          f1++;
       }else if (a[i]<0){
          f2++;
       }else
       f3++;
    }
    printf(“%f\n%f\n%f\n”,f1/n,f2/n,f3/n);
    return 0;
}
#include <stdio.h>

 

int main(){
    int a,i,j,k,l;
    scanf(“%d”,&a);
    for(i=1;i<a+1;i++)
        {
        for(k=1;k<=a-i;k++)
            {
            printf(” “);
            }
        for(l=i;l>0;l–)
            {
            printf(“#”);
        }
        printf(“\n”);
        }
    return 0;
}

A Very Big Sum

Calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

#include <stdio.h>

int main() {
    int t,i;
   scanf("%d",&t);
    long int arr[1000];
    long int s=0;
    for(i=0;i<t;i++)
        {
        scanf("%ld",&arr[i]);
    }
    for(i=0;i<t;i++)
        {
        s=s+arr[i];
    }
    printf("%ld",s);
    return 0;
}

Time Conversion

Given a time in 12-hour AM/PM format, convert it to military (24-hour) time.

Note: – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

#include<stdio.h>
#include<string.h>
int main(){
    int hh,mm,ss;
    char t12[3];
    scanf(“%d:%d :%d%s”,&hh,&mm,&ss,t12);
    if(strcmp (t12 ,”PM”)==0 && hh!=12 ) hh+= 12;
    if(strcmp (t12 ,”AM”)==0 && hh==12 ) hh = 0;
    printf(“%02d:%02d:%02d”, hh,mm,ss);
    return 0;
}

Grading Students

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

HackerLand University has the following grading policy:

  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.

Sam is a professor at the university and likes to round each student’s  according to these rules:

  • If the difference between the  and the next multiple of  is less than , round  up to the next multiple of .
  • If the value of  is less than , no rounding occurs as the result will still be a failing grade.
int main()
{
   int n,mark,grade,i;
   scanf(“%d”,&n);
   for(i=0;i<n;i++){
    scanf(“%d”,&grade);
    if(grade<38){
     printf(“%d\n”,grade);
     continue;
    }
    if((grade+2)%5==0|| (grade+1)%5==0)
        grade=((grade+2)%5==0)?grade+2:grade+1;
        printf(“%d\n”,grade);
   }
    return 0;
}