Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Sort two arrays. array1
ascending, array2
descending. Add each ith
element in both array and compare if their sum is greater or equals than required amount.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.*; public class TwoArrays { private static class Descending implements Comparator<Integer> { @Override public int compare(Integer i, Integer j) { return j - i; } } public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = stdin.nextInt(); for(int i = 0; i < tests; i++) { int n = stdin.nextInt(); int sum = stdin.nextInt(); Integer[] array1 = new Integer[n]; Integer[] array2 = new Integer[n]; for(int j = 0; j < n; j++){ array1[j] = stdin.nextInt(); } for(int j = 0; j < n; j++){ array2[j] = stdin.nextInt(); } Arrays.sort(array1); Arrays.sort(array2, new Descending()); System.out.println(isCorrect(array1, array2, sum)); } stdin.close(); } private static String isCorrect(Integer[] array1, Integer[] array2, int sum) { for(int i = 0; i < array1.length; i++) { if(array1[i] + array2[i] < sum) { return "NO"; } } return "YES"; } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
'use strict'; let isCorrect = (arr1, arr2, sum) => { for(let i = 0; i < arr1.length; i++) { if(arr1[i] + arr2[i] < sum) { return "NO"; } } return "YES"; }; const processData = input => { let lines = input.split('\n'); let tests = parseInt(lines[0]); let index = 1; for(let i = 0; i < tests; i++) { let def = lines[index++].split(' ').map(i => parseInt(i)); let n = def[0]; let sum = def[1]; let arr1 = lines[index++].split(' ').map(i => parseInt(i)).sort((i, j) => i - j); let arr2 = lines[index++].split(' ').map(i => parseInt(i)).sort((i, j) => j - i); console.log(isCorrect(arr1, arr2, sum)); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import scala.io.Source object TwoArrays extends App { val console = Source.stdin.bufferedReader() val tests = console.readLine().toInt val descendingSort = (i: Int, j: Int) => i > j (0 until tests).foreach(i => { val line = console.readLine() val numbers = line.split(" ").map(_.toInt) val n = numbers(0) val sum = numbers(1) val array1 = console.readLine().split(" ").map(_.toInt) val array2 = console.readLine().split(" ").map(_.toInt) val sortedArray1 = array1.sorted val sortedArray2 = array2.sortWith(descendingSort) println(prove(sortedArray1, sortedArray2, sum)) }) def prove(arr1: Array[Int], arr2: Array[Int], sum: Int): String = { val correct = arr1.indices.forall(i => {arr1(i) + arr2(i) >= sum}) if(correct) "YES" else "NO" } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def prove(arr1, arr2, sum) for i in 0..arr1.length - 1 do return "NO" if arr1[i] + arr2[i] < sum end return "YES" end test_cases = gets.chomp.to_i test_cases.times do (arr_length, sum) = gets.chomp.split.map { |e| e.to_i} arr1 = gets.chomp.split.map { |e| e.to_i} arr2 = gets.chomp.split.map { |e| e.to_i} arr1.sort! {|i, j| i <=> j} arr2.sort! { |i, j| j <=> i} puts prove(arr1, arr2, sum) end |