`{"id":"61ff9eda-c57d-4b5b-bb89-5dc1e816d789","name":"Quadruplet Sum","description":"1. You are given an array(arr) of N integers and an integer X.\r\n2. You have to find all unique quadruplets(a,b,c,d) which satisfies this condition - \r\n a+b+c+d = X.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number X","outputFormat":"Every line of output contains four integers which represent a quadruplet having sum X.","constraints":"1 &lt;= N &lt;= 200\r\n-10^3 &lt;= arr[i] &lt;= 10^3\r\n-10^9 &lt;= X &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <algorithm>\r\n\r\nusing namespace std;\r\n\r\nvector<vector<int>> fourSum(vector<int> &nums, int target, int n)\r\n{\r\n vector<vector<int>> res;\r\n return res;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n cin >> arr[i];\r\n\r\n int target;\r\n cin >> target;\r\n vector<vector<int>> ans = fourSum(arr, target, n);\r\n sort(ans.begin(), ans.end(), [](auto &A, auto &B) {\r\n int i = 0;\r\n int j = 0;\r\n\r\n while (i < A.size())\r\n {\r\n if (A[i] == B[j])\r\n {\r\n i++;\r\n j++;\r\n }\r\n else\r\n return A[i] < B[j];\r\n }\r\n\r\n return true;\r\n });\r\n for (auto &a : ans)\r\n {\r\n for (int ele : a)\r\n {\r\n cout << ele << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}\r\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static ArrayList<ArrayList<Integer>> fourSum(int[] nums, int target, int n) {\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint n = sc.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\tarr[i] = sc.nextInt();\r\n\t\t}\r\n\t\tint target = sc.nextInt();\r\n\t\tArrayList<ArrayList<Integer>> ans = fourSum(arr, target, n);\r\n\t\tCollections.sort(ans,(a,b)->{\r\n int i = 0;\r\n int j = 0;\r\n \r\n while(i < a.size()){\r\n if(a.get(i) == b.get(j)){\r\n i++;\r\n j++;\r\n }else{\r\n return a.get(i) - b.get(j);\r\n }\r\n }\r\n \r\n return 0;\r\n }); \r\n\t\tfor (ArrayList<Integer> a : ans) {\r\n\t\t\tfor (int element : a) {\r\n\t\t\t\tSystem.out.print(element + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n}\r\n"},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 0 -1 0 -2 2\r\n0","sampleOutput":"-2 -1 1 2 \r\n-2 0 0 2 \r\n-1 0 0 1 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"68877e72-9a05-4599-bb8b-2aff79fbf098","name":"Quadruplet Sum","slug":"quadruplet-sum","type":1}],"next":{"id":"a3fe4427-251f-4157-9ce8-8f26c76079b3","name":"Quadruplet Sum MCQ","type":0,"slug":"quadruplet-sum-mcq"},"prev":{"id":"6a1add7f-bea1-4121-8777-9f34e697168a","name":"Pairs With Given Sum In Two Sorted Matrices","type":3,"slug":"pairs-with-given-sum-in-two-sorted-matrices"}}}`

1. You are given an array(arr) of N integers and an integer X. 2. You have to find all unique quadruplets(a,b,c,d) which satisfies this condition - a+b+c+d = X.

`{"id":"61ff9eda-c57d-4b5b-bb89-5dc1e816d789","name":"Quadruplet Sum","description":"1. You are given an array(arr) of N integers and an integer X.\r\n2. You have to find all unique quadruplets(a,b,c,d) which satisfies this condition - \r\n a+b+c+d = X.","inputFormat":"A number N\r\narr1\r\narr2.. N numbers\r\nA number X","outputFormat":"Every line of output contains four integers which represent a quadruplet having sum X.","constraints":"1 &lt;= N &lt;= 200\r\n-10^3 &lt;= arr[i] &lt;= 10^3\r\n-10^9 &lt;= X &lt;= 10^9","sampleCode":{"cpp":{"code":"#include <iostream>\r\n#include <vector>\r\n#include <algorithm>\r\n\r\nusing namespace std;\r\n\r\nvector<vector<int>> fourSum(vector<int> &nums, int target, int n)\r\n{\r\n vector<vector<int>> res;\r\n return res;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n vector<int> arr(n, 0);\r\n for (int i = 0; i < n; i++)\r\n cin >> arr[i];\r\n\r\n int target;\r\n cin >> target;\r\n vector<vector<int>> ans = fourSum(arr, target, n);\r\n sort(ans.begin(), ans.end(), [](auto &A, auto &B) {\r\n int i = 0;\r\n int j = 0;\r\n\r\n while (i < A.size())\r\n {\r\n if (A[i] == B[j])\r\n {\r\n i++;\r\n j++;\r\n }\r\n else\r\n return A[i] < B[j];\r\n }\r\n\r\n return true;\r\n });\r\n for (auto &a : ans)\r\n {\r\n for (int ele : a)\r\n {\r\n cout << ele << \" \";\r\n }\r\n cout << endl;\r\n }\r\n\r\n return 0;\r\n}\r\n"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n public static ArrayList<ArrayList<Integer>> fourSum(int[] nums, int target, int n) {\r\n\t\t// write your code here\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner sc = new Scanner(System.in);\r\n\t\tint n = sc.nextInt();\r\n\t\tint[] arr = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\tarr[i] = sc.nextInt();\r\n\t\t}\r\n\t\tint target = sc.nextInt();\r\n\t\tArrayList<ArrayList<Integer>> ans = fourSum(arr, target, n);\r\n\t\tCollections.sort(ans,(a,b)->{\r\n int i = 0;\r\n int j = 0;\r\n \r\n while(i < a.size()){\r\n if(a.get(i) == b.get(j)){\r\n i++;\r\n j++;\r\n }else{\r\n return a.get(i) - b.get(j);\r\n }\r\n }\r\n \r\n return 0;\r\n }); \r\n\t\tfor (ArrayList<Integer> a : ans) {\r\n\t\t\tfor (int element : a) {\r\n\t\t\t\tSystem.out.print(element + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n}\r\n"},"node":{"code":""},"ruby":{"code":""},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6\r\n1 0 -1 0 -2 2\r\n0","sampleOutput":"-2 -1 1 2 \r\n-2 0 0 2 \r\n-1 0 0 1 \r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"ec6515dc-6811-4c65-912f-0134b65df120","name":"HashMap and Heap For Intermediate","slug":"hashmap-and-heap-for-intermediate-10002","type":0},{"id":"68877e72-9a05-4599-bb8b-2aff79fbf098","name":"Quadruplet Sum","slug":"quadruplet-sum","type":1}],"next":{"id":"a3fe4427-251f-4157-9ce8-8f26c76079b3","name":"Quadruplet Sum MCQ","type":0,"slug":"quadruplet-sum-mcq"},"prev":{"id":"6a1add7f-bea1-4121-8777-9f34e697168a","name":"Pairs With Given Sum In Two Sorted Matrices","type":3,"slug":"pairs-with-given-sum-in-two-sorted-matrices"}}}` Editor

medium

1. You are given an array(arr) of N integers and an integer X. 2. You have to find all unique quadruplets(a,b,c,d) which satisfies this condition - a+b+c+d = X.

## Constraints

1 <= N <= 200 -10^3 <= arr[i] <= 10^3 -10^9 <= X <= 10^9

## Format

### Input

A number N arr1 arr2.. N numbers A number X

### Output

Every line of output contains four integers which represent a quadruplet having sum X.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}6 1 0 -1 0 -2 2 0```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}-2 -1 1 2 -2 0 0 2 -1 0 0 1 ```

Discussions

Show Discussion

Related Resources 