Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // Count Quadruplets - JAVA
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- int m = sc.nextInt();
- int p = sc.nextInt();
- int q = sc.nextInt();
- int target = sc.nextInt();
- ArrayList<Integer> a = new ArrayList<>();
- ArrayList<Integer> b = new ArrayList<>();
- ArrayList<Integer> c = new ArrayList<>();
- ArrayList<Integer> d = new ArrayList<>();
- for(int i = 0;i < n; i++){
- a.add(sc.nextInt());
- }
- for(int i = 0;i < m; i++){
- b.add(sc.nextInt());
- }
- for(int i = 0;i < p; i++){
- c.add(sc.nextInt());
- }
- for(int i = 0;i < q; i++){
- d.add(sc.nextInt());
- }
- System.out.println(countQuadruplets(a, b, c, d, target));
- }
- public static int countQuadruplets(ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> c, ArrayList<Integer> d, int target) {
- HashMap<Integer, Integer> map = new HashMap<>();
- for (int i : a) {
- for (int j : b) {
- if (map.containsKey(i + j)) {
- map.put(i + j, map.get(i + j) + 1);
- } else
- map.put(i + j, 1);
- }
- }
- int count = 0;
- for (int i : c) {
- for (int j : d) {
- if (map.containsKey(target - i - j)) {
- count += map.get(target - i - j);
- }
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement