Permuting Two Arrays  hackerrank solution.  The problem is simple given at https://www.hackerrank.com/challenges/two-arrays . It requires sorting. we can use qsort standard library function. To know more about qsort visit at http://wikistack.com/quick-sort-algorithm-in-glibc-libraryqsort/ .

Permuting Two Arrays  hackerrank solution

#include<iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int ascen(const void * a, const void * b) {
	return (*(int*) a - *(int*) b);
}

int desen(const void * a, const void * b) {
	return (*(int*) b - *(int*) a);
}

int A[1001];
int B[1001];

int main() {
	freopen("input.txt", "r", stdin);

	int qn = 0;

	cin >> qn;

	for (int tc = 0; tc < qn; tc++) {
		int len_of_array = 0;
		int k = 0;

		cin >> len_of_array >> k;

		for (int i = 0; i < len_of_array; i++) {
			cin >> A[i];
		}

		for (int i = 0; i < len_of_array; i++) {
			cin >> B[i];
		}

		qsort(A, len_of_array, sizeof(int), desen);
		qsort(B, len_of_array, sizeof(int), ascen);

		bool f = true;
		for (int i = 0; i < len_of_array; i++) {
			if (A[i] + B[i] < k) {
				f = false;
			}
		}

		if(f)
		cout << "YES" << endl;
	    else
	    cout << "NO" << endl;
}

 




Related Contents to follow