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
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 .
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.
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.
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
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.
Grading Students
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.