Advertisement
saurav_kalsoor

Count Quadruplets - JAVA

Jul 18th, 2022
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  2. // Count Quadruplets - JAVA
  3.  
  4. import java.util.*;
  5.  
  6. public class Test {
  7.     static Scanner sc = new Scanner(System.in);
  8.     public static void main(String[] args) {
  9.         int n = sc.nextInt();
  10.         int m = sc.nextInt();
  11.         int p = sc.nextInt();
  12.         int q = sc.nextInt();
  13.         int target = sc.nextInt();
  14.  
  15.         ArrayList<Integer> a = new ArrayList<>();
  16.         ArrayList<Integer> b = new ArrayList<>();
  17.         ArrayList<Integer> c = new ArrayList<>();
  18.         ArrayList<Integer> d = new ArrayList<>();
  19.  
  20.         for(int i = 0;i < n; i++){
  21.             a.add(sc.nextInt());
  22.         }
  23.  
  24.         for(int i = 0;i < m; i++){
  25.             b.add(sc.nextInt());
  26.         }
  27.  
  28.         for(int i = 0;i < p; i++){
  29.             c.add(sc.nextInt());
  30.         }
  31.  
  32.         for(int i = 0;i < q; i++){
  33.             d.add(sc.nextInt());
  34.         }
  35.         System.out.println(countQuadruplets(a, b, c, d, target));
  36.     }
  37.  
  38.     public static int countQuadruplets(ArrayList<Integer> a, ArrayList<Integer> b, ArrayList<Integer> c, ArrayList<Integer> d, int target) {
  39.         HashMap<Integer, Integer> map = new HashMap<>();
  40.        
  41.         for (int i : a) {
  42.             for (int j : b) {
  43.                 if (map.containsKey(i + j)) {
  44.                     map.put(i + j, map.get(i + j) + 1);
  45.                 } else
  46.                     map.put(i + j, 1);
  47.             }
  48.         }
  49.        
  50.         int count = 0;
  51.        
  52.         for (int i : c) {
  53.             for (int j : d) {
  54.                 if (map.containsKey(target - i - j)) {
  55.                     count += map.get(target - i - j);
  56.                 }
  57.             }
  58.         }
  59.        
  60.         return count;
  61.     }
  62.    
  63. }
  64.  
  65.  
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement