SHOW:
|
|
- or go back to the newest paste.
| 1 | // Author : Saurav Kalsoor | |
| 2 | - | // Count Quadruplets - JAVA |
| 2 | + | // Count Quadruplets - KOTLIN |
| 3 | ||
| 4 | - | import java.util.*; |
| 4 | + | import java.util.* |
| 5 | ||
| 6 | - | public class Test {
|
| 6 | + | var sc = Scanner(System.`in`) |
| 7 | - | static Scanner sc = new Scanner(System.in); |
| 7 | + | |
| 8 | - | public static void main(String[] args) {
|
| 8 | + | fun main() {
|
| 9 | - | int n = sc.nextInt(); |
| 9 | + | val n = sc.nextInt() |
| 10 | - | int m = sc.nextInt(); |
| 10 | + | val m = sc.nextInt() |
| 11 | - | int p = sc.nextInt(); |
| 11 | + | val p = sc.nextInt() |
| 12 | - | int q = sc.nextInt(); |
| 12 | + | val q = sc.nextInt() |
| 13 | - | int target = sc.nextInt(); |
| 13 | + | val target = sc.nextInt() |
| 14 | ||
| 15 | - | ArrayList<Integer> a = new ArrayList<>(); |
| 15 | + | val a = ArrayList<Int>() |
| 16 | - | ArrayList<Integer> b = new ArrayList<>(); |
| 16 | + | val b = ArrayList<Int>() |
| 17 | - | ArrayList<Integer> c = new ArrayList<>(); |
| 17 | + | val c = ArrayList<Int>() |
| 18 | - | ArrayList<Integer> d = new ArrayList<>(); |
| 18 | + | val d = ArrayList<Int>() |
| 19 | ||
| 20 | - | for(int i = 0;i < n; i++){
|
| 20 | + | for (i in 0 until n) {
|
| 21 | - | a.add(sc.nextInt()); |
| 21 | + | a.add(sc.nextInt()) |
| 22 | - | } |
| 22 | + | |
| 23 | for (i in 0 until m) {
| |
| 24 | - | for(int i = 0;i < m; i++){
|
| 24 | + | b.add(sc.nextInt()) |
| 25 | - | b.add(sc.nextInt()); |
| 25 | + | |
| 26 | - | } |
| 26 | + | for (i in 0 until p) {
|
| 27 | c.add(sc.nextInt()) | |
| 28 | - | for(int i = 0;i < p; i++){
|
| 28 | + | |
| 29 | - | c.add(sc.nextInt()); |
| 29 | + | for (i in 0 until q) {
|
| 30 | - | } |
| 30 | + | d.add(sc.nextInt()) |
| 31 | } | |
| 32 | - | for(int i = 0;i < q; i++){
|
| 32 | + | println(countQuadruplets(a, b, c, d, target)) |
| 33 | - | d.add(sc.nextInt()); |
| 33 | + | |
| 34 | - | } |
| 34 | + | |
| 35 | - | System.out.println(countQuadruplets(a, b, c, d, target)); |
| 35 | + | fun countQuadruplets(a: ArrayList<Int>, b: ArrayList<Int>, c: ArrayList<Int>, d: ArrayList<Int>, target: Int): Int {
|
| 36 | val map = HashMap<Int, Int>() | |
| 37 | for (i in a) {
| |
| 38 | - | public static int countQuadruplets(ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> c, ArrayList<Integer> d, int target) {
|
| 38 | + | for (j in b) {
|
| 39 | - | HashMap<Integer, Integer> map = new HashMap<>(); |
| 39 | + | if (map.containsKey(i + j)) {
|
| 40 | - | |
| 40 | + | map[i + j] = map[i + j]!! + 1 |
| 41 | - | for (int i : a) {
|
| 41 | + | } else map[i + j] = 1 |
| 42 | - | for (int j : b) {
|
| 42 | + | |
| 43 | - | if (map.containsKey(i + j)) {
|
| 43 | + | |
| 44 | - | map.put(i + j, map.get(i + j) + 1); |
| 44 | + | var count = 0 |
| 45 | - | } else |
| 45 | + | for (i in c) {
|
| 46 | - | map.put(i + j, 1); |
| 46 | + | for (j in d) {
|
| 47 | if (map.containsKey(target - i - j)) {
| |
| 48 | count += map[target - i - j]!! | |
| 49 | - | |
| 49 | + | |
| 50 | - | int count = 0; |
| 50 | + | |
| 51 | - | |
| 51 | + | |
| 52 | - | for (int i : c) {
|
| 52 | + | return count |
| 53 | - | for (int j : d) {
|
| 53 | + | |
| 54 | - | if (map.containsKey(target - i - j)) {
|
| 54 | + | |
| 55 | - | count += map.get(target - i - j); |
| 55 | + | |
| 56 | - | } |
| 56 | + |