https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/copy-from/61462098
int co(const void *p, const void *q)
{
return *(int *)p-*(int *)q;
}
int minimumAbsoluteDifference(int n, int arr_size, int* arr)
{
int min,temp;
qsort(arr,n,sizeof(int),co);//to sort in ascending order
min=abs(arr[0]-arr[1]);
for(int i=1;i<n-1;i++)
{
int tmp=abs(arr[i]-arr[i+1]);
if(min>tmp)
min=tmp;
}
return min;
}
==============================================
https://www.hackerrank.com/challenges/marcs-cakewalk/problem
#include <math.h>
#include <stdio.h>
int co(const void *p, const void *q)
{
return *(int *)q-*(int *)p;
}
int main(){
int n,i;
unsigned long int sum=0;
scanf("%d",&n);
int *calories = malloc(sizeof(int) * n);
for(int calories_i = 0; calories_i < n; calories_i++){
scanf("%d",&calories[calories_i]);
}
qsort(calories,n,sizeof(int),co);//sort in descending order
for(i=0;i<n;i++)
sum=sum+(calories[i]*pow(2,i));
printf("%ld",sum);
// your code goes here
return 0;
}
=================================
https://www.hackerrank.com/challenges/pairs/copy-from/66162811
Pairs Hacker Rank Solution in C
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int co(const void *p, const void *q)
{
return *(int *)p-*(int *)q;
}
int pairs(int k, int n, int* arr)
{
int count =0,i=0;
for(int j =1;j<n;)
{
int var=abs(arr[j]-arr[i]);
if(var == k)
{
count++;
j++;
}
else if(var>k)
i++;
else
j++;
}
return count;
}
int main() {
int n;
int k;
scanf("%i %i", &n, &k);
int *arr = malloc(sizeof(int) * n);
for (int arr_i = 0; arr_i < n; arr_i++) {
scanf("%i",&arr[arr_i]);
}
qsort(arr,n,sizeof(int),co);
int result = pairs(k, n, arr);
printf("%d\n", result);
return 0;
}
==================================================
https://www.codechef.com/CODR2018/problems/KRYP4
"The blood of House of El will always bind us together."
What the Kryptonians need is to be together, to be united in order to save the apocalypse.
For that, Seg-El has decided to carry out some encoding. The people of the same behavior if made to stay together will lead to enhanced protection of Krypton.
There are N people on Krypton. Each has a behavior A[i] associated with him/her.
You need to tell Seg-El the total number of ways to choose any two people of same behavior so that they can be made to stay together.
Input
First line consists of N - the number of people.
Second line comprises N space separated integers denoting A[i].
Output
Print the answer to the number of ways to place 2 people together having same behaviour.
Constraints
- 1 ≤ N ≤ 100000
- 1 ≤ A[i] ≤ 1015
Example
Input:
4
1 1 2 2
Output:
2
Explanation
There is only 1 way in which 2 people with behavior 1 can be together. Similarly, for behavior 2 people. Hence, total number of ways are 2.
Solution:
#include<stdio.h>
int cmpfunc (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
int main()
{
long long t;
scanf("%lld",&t);
long long a[t];
for(long long i=0;i<t;i++)
{
scanf("%lld",&a[i]);
}
qsort(a,t,sizeof(long long),cmpfunc);
long long count=0;
long long sum=0;
long long temp=a[0];
for(long long i=0;i<t;i++)
{
if(temp==a[i])
count++;
else
{
temp=a[i];
if(count>=2)
sum+=(count*(count-1)/2);
count=1;
}
}
if(count>=2)
{
sum+=(count*(count-1)/2);
}
printf("%lld\n",sum);
}
================================
problem : https://www.hackerrank.com/challenges/minimum-loss/
Solution:
struct h
{
long int hp,in;
};
int co(const void *p, const void *q)
{
struct h *x = (struct h *) p;
struct h *y = (struct h *) q;
if (x->hp < y->hp)
return -1;
else if (x->hp > y->hp)
return 1;
return 0;
}//ascending order sorting
long int minimumLoss(int n, long* price) {
long int i,j,min=LONG_MAX,diff;
struct h h1[n];
for(i=0;i<n;i++)
{
h1[i].hp=price[i];
h1[i].in=i;
}
qsort(h1,n,sizeof(struct h),co);
for(i=0;i<n-1;i++)
{
diff=h1[i+1].hp - h1[i].hp;
if(min>diff &&(h1[i+1].in < h1[i].in))
min=diff;
}
return min;
}