Hackerrank – Problem description
The problem description – Hackerrank.
Solution
I created an array of all positive numbers, which satisfy conditions specified in problem statement. This set should contain elements from to . I filter all numbers from the set that are valid for an equation a .
The array of positive numbers can be reduced. It could only contain numbers ranging from to (included). If an arbitrary number is divisible with – all elements less than are not divisible without a remainder. The same holds for . There would be a remainder for all numbers greater than
.
I created solution in:
All solutions are also available on my GitHub profile.
Scala
1 2 3 4 5 6 7 8 9 10 11 |
import scala.io.Source object BetweenTwoSets extends App { val lines = Source.stdin.getLines().toList val a = lines(1).split(" ").map(_.toInt) val b = lines(2).split(" ").map(_.toInt) val allNumbers = (a.max to b.min).toList val x = allNumbers.filter(num => a.forall(i => num % i == 0) && b.forall(j => j % num == 0)) println(x.size) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class BetweenTwoSets { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String firstLine = stdin.nextLine(); List<integer> a = Arrays.stream(stdin.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList()); List<integer> b = Arrays.stream(stdin.nextLine().split(" ")).map(Integer::parseInt).collect(Collectors.toList()); int aMax = a.stream().max((Integer o1, Integer o2) -> o1 - o2).get(); int bMin = b.stream().min((Integer o1, Integer o2) -> o1 - o2).get(); List<integer> result = IntStream.range(aMax, bMin + 1).filter(num -> a.stream().allMatch(i -> num % i == 0) && b.stream().allMatch(j -> j % num == 0)) .mapToObj(i -> i).collect(Collectors.toList()); System.out.println(result.size()); stdin.close(); } } </integer></integer></integer> |
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 |
'use strict'; const range = (start, end) => { const ret = []; for(let i = start; i <= end; i++) { ret.push(i); } return ret; } const processData = input => { const lines = input.split('\n'); const a = lines[1].split(' ').map(i => parseInt(i)); const b = lines[2].split(' ').map(i => parseInt(i)); const aMax = Math.max(...a); const bMin = Math.min(...b); const result = range(aMax, bMin).filter(num => a.every(i => num % i === 0) && b.every(j => j % num === 0)); console.log(result.length); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 |
(n, m) = gets.split.map(&:to_i) a = gets.split.map(&:to_i) b = gets.split.map(&:to_i) a_max = a.max b_min = b.min all_numbers = (a_max..b_min).to_a result = all_numbers.select do |num| factor_of_a = a.all? { |i| num % i == 0 } factor_of_b = b.all? { |j| j % num == 0 } factor_of_a && factor_of_b end puts result.size |