Hackerrank Algorithmic Crush Solution in Python

from heapq import *

class Node(object):
	def __init__(self, l):
		super(Node, self).__init__()
		# Start, Finish and Weight
		self.s = l[0]; self.f = l[1]; self.w = l[2]
	def __lt__(self, o):
		return self.f < o.f

N, M = map(int, raw_input().split())
A = []; heap = []

for m in xrange(M):
	A.append(Node(map(int, raw_input().split())))

A.sort(key = lambda x: x.s)

maxi = 0; m = 0
for a in A:
	while len(heap)>0 and heap[0].f<a.s:
		m -= heappop(heap).w
	m += a.w
	if m>maxi: maxi = m
	heappush(heap, a)

print maxi

Sample Input:

5 3
1 2 100
2 5 100
3 4 100

Sample Output:

200

Hackerrank Chief Hopper Solution in C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

int main() {
    int N; scanf("%d", &N);
    vector <ll> Arr(N);
    for (int i=0;i<N;i++) {
        scanf("%lld", &Arr[i]);
    }
    ll ans = 0;
    for (int i = N-1;i>=0;i--) {
        ans = (Arr[i] + ans)/2 + (Arr[i] + ans)%2;
    }
    printf("%lld\n", ans);
    return 0;
}

Sample Input:

5
3 4 3 2 4

Sample Output:

4

Hackerrank Priyanka and Toys Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;

int main(){
	int N; scanf("%d", &N);
	priority_queue<int, vector<int>, greater<int> > q;
	int s;
	for(int n = 0; n<N; n++){
		scanf("%d", &s);
		q.push(s);
	}
	int current = 0, count = 0;
	while(!q.empty()){
		current = q.top(); q.pop();
		while(q.top()<=current+4 && !q.empty()) q.pop();
		count++;
	}
	if(count>0) printf("%d\n", count);
	else printf("0\n");
	return 0;
}

Sample Input:

5
1 2 3 17 10

Sample Output:

3

Hackerrank Two Arrays Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;

int main(){
	int N, K; scanf("%d %d", &N, &K);
	priority_queue<int, vector<int>, greater<int> > q;
	int s;
	for(int n = 0; n<N; n++){
		scanf("%d", &s);
		q.push(s);
	}
	int sum = 0, count = -1;
	while(sum<K && !q.empty()){
		sum += q.top(); q.pop();
		count++;
	}
	if(count>0) printf("%d\n", count);
	else printf("0\n");
	return 0;
}

Sample Input:

2
3 10
2 1 3
7 8 9
4 5
1 2 2 1
3 3 3 4

Sample Output:

YES
NO

Hackerrank Mark and Toys Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;

int main(){
	int N, K; scanf("%d %d", &N, &K);
	priority_queue<int, vector<int>, greater<int> > q;
	int s;
	for(int n = 0; n<N; n++){
		scanf("%d", &s);
		q.push(s);
	}
	int sum = 0, count = -1;
	while(sum<K && !q.empty()){
		sum += q.top(); q.pop();
		count++;
	}
	if(count>0) printf("%d\n", count);
	else printf("0\n");
	return 0;
}

Sample Input:

7 50
1 12 5 111 200 1000 10

Sample Output:

4

Hackerrank Jim and the Orders Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

int main(){
	int N; scanf("%d", &N);
	vector<pair<int, int> > A;
	int s, f;
	for(int n = 0; n<N; n++){
		scanf("%d %d", &s, &f);
		A.push_back(make_pair(s+f, n));
	}
	sort(A.begin(), A.end());
	for(int n = 0; n<N; n++){
		printf("%d ", A[n].second+1);
	}
	return 0;
}

Sample Input:

3
1 3
2 3
3 3

Sample Output:

1 2 3

Hackerrank Grid Challenge Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

int main(){
	int T; scanf("%d", &T);
	while(T--){
		int N; scanf("%d", &N);
		char A[N][N];
		for(int n = 0; n<N; n++){
			scanf("%s", A[n]);
			sort(A[n], A[n]+N);
		}
		bool b = false;
		for(int i = 0; i<N; i++){
			if(b) break;
			for(int j = 0; j<N-1; j++){
				if(A[j+1][i]<A[j][i]){
					b = true;
					break;
				}
			}
		}
		if(!b) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

Sample Input:

1
5
ebacd
fghij
olmkn
trpqs
xywuv

Sample Output:

YES

Hackerrank Flowers Solution in C++

#include<cstdio>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>

using namespace std;
#define p(x) printf("%d\n", x)

int main(){
	int N, K, s; scanf("%d %d", &N, &K);
	priority_queue<int> pq;
	for(int n = 0; n<N; n++) scanf("%d", &s), pq.push(s);
	int x = 1, iter = 0, sum = 0;
	while(!pq.empty()){
		sum += x*pq.top();
		pq.pop();
		iter++;
		if(iter%K==0) x++;
	}
	p(sum);
	return 0;
}

Sample Input:

3 3
2 5 6

Sample Output:

13

HackerRank Pairs Solution in C++

Given N integers, count the number of pairs of integers whose difference is K.

First the array of integers is sorted and then binary search is applied for the value of +K of the given indexed value of the array. This will work because all the values in the array are distinct.

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
	int N, K;
	scanf("%d %d", &N, &K);
	int a[N];
	for(int n = 0; n<N; n++){
		scanf("%d", &a[n]);
	}
	sort(a, a+N);
	int count = 0;
	for(int n = 0; n<N; n++)
		if(binary_search(a, a+N, a[n]+K)) count++;
	printf("%d", count);
}

Sample Input:

10 1  
363374326 364147530 61825163 1073065718 1281246024 1399469912 428047635 491595254 879792181 1069262793

Sample Output:

0

Runtime: 0.02s

HackerRank Missing Numbers Solution in C++

Simple frequency counting as done in counting sort and then subtracting the frequency of the given number in other list. Printing the number times the frequency for the remaining numbers.

Things to note: The range of the values of x.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

#define SIZE 10001
using namespace std;


int main() {
    int a[SIZE];
    for(int i = 0; i<SIZE;i++){
        a[i]=0;
    }
    int N, M;
    scanf("%d", &N);
    int temp;
    for(int n=0; n<N; n++){
        scanf("%d", &temp);
        a[temp]+=1;
    }
    scanf("%d", &M);
    for(int m =0; m<M; m++){
        scanf("%d", &temp);
        a[temp]-=1;
    }
    for(int i =0; i<SIZE; i++){
        if(a[i]<0) printf("%d ", i);
    }
    return 0;
}

Sample Input:

10
203 204 205 206 207 208 203 204 205 206
13
203 204 204 205 206 207 205 208 203 206 205 206 204

Sample Output:

204 205 206

Runtime: 0.03s