Here's the implementation of the fourSum function and corresponding driver code in Python, Java, C#, and JavaScript according to your instructions:
Python
def fourSum(arr, target):
n = len(arr)
mp = {}
# Ideally we should us an unordered_set here, but Python's set supports complex objects natively.
# So we have used set to keep the code simple.
resset = set()
# Store all pairs for every possible pairs sum
for i in range(n):
for j in range(i + 1, n):
sum = arr[i] + arr[j]
if sum not in mp:
mp[sum] = []
mp[sum].append((i, j))
# Pick the first two elements
for i in range(n - 1):
for j in range(i + 1, n):
# Find pairs with the remaining sum
rem = target - arr[i] - arr[j]
if rem in mp:
pairs = mp[rem]
for p in pairs:
# Ensure no two indexes are same in a quaduple and
# all indexes are in sorted order
if p[0] != i and p[1] != i and p[0] != j and p[1] != j:
curr = [p[0], p[1], i, j]
curr.sort()
resset.add(tuple(curr))
res = [list(quad) for quad in resset]
return res
# Driver Code
arr = [1, 1, 1, 1, 1]
target = 4
res = fourSum(arr, target)
for quad in res:
print(" ".join(map(str, quad)))
Java
import java.util.*;
class GfG {
static List<List<Integer>> fourSum(int[] arr, int target) {
int n = arr.length;
Map<Integer, List<int[]>> mp = new HashMap<>();
// Ideally we should us an unordered_set here, but Java
// does not support vector as a key in an HashSet
// So we have used TreeSet to keep the code simple. However
// set internally uses Red Black Tree and has O(Log n)
// time complexities for operations
Set<List<Integer>> resset = new TreeSet<>((a, b) -> {
for (int i = 0; i < a.size(); i++) {
if (!a.get(i).equals(b.get(i))) {
return a.get(i) - b.get(i);
}
}
return 0;
});
// Store all pairs for every possible pairs sum
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
mp.computeIfAbsent(sum, k -> new ArrayList<>()).add(new int[]{i, j});
}
}
// Pick the first two elements
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Find pairs with the remaining sum
int rem = target - arr[i] - arr[j];
if (mp.containsKey(rem)) {
List<int[]> pairs = mp.get(rem);
for (int[] p : pairs) {
// Ensure no two indexes are same in a quaduple and
// all indexes are in sorted order
if (p[0] != i && p[1] != i && p[0] != j && p[1] != j) {
List<Integer> curr = Arrays.asList(p[0], p[1], i, j);
Collections.sort(curr);
resset.add(curr);
}
}
}
}
}
return new ArrayList<>(resset);
}
// Driver Code
public static void main(String[] args) {
int[] arr = {1, 1, 1, 1, 1};
int target = 4;
List<List<Integer>> res = fourSum(arr, target);
for (List<Integer> quad : res) {
System.out.println(quad);
}
}
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GfG {
static List<List<int>> FourSum(List<int> arr, int target) {
int n = arr.Count;
var mp = new Dictionary<int, List<Tuple<int, int>>>();
// Ideally we should us an unordered_set here, but C#
// does not support vector as a key in an HashSet.
// So we have used SortedSet to keep the code simple.
var resset = new SortedSet<List<int>>(Comparer<List<int>>.Create((a, b) => {
for (int i = 0; i < a.Count; i++) {
if (a[i] != b[i]) return a[i] < b[i] ? -1 : 1;
}
return 0;
}));
// Store all pairs for every possible pairs sum
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
if (!mp.ContainsKey(sum)) mp[sum] = new List<Tuple<int, int>>();
mp[sum].Add(Tuple.Create(i, j));
}
}
// Pick the first two elements
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// Find pairs with the remaining sum
int rem = target - arr[i] - arr[j];
if (mp.ContainsKey(rem)) {
List<Tuple<int, int>> pairs = mp[rem];
foreach (var p in pairs) {
// Ensure no two indexes are same in a quaduple and
// all indexes are in sorted order
if (p.Item1 != i && p.Item2 != i && p.Item1 != j && p.Item2 != j) {
List<int> curr = new List<int> { p.Item1, p.Item2, i, j };
curr.Sort();
resset.Add(curr);
}
}
}
}
}
return resset.ToList();
}
// Driver Code
static void Main() {
List<int> arr = new List<int> { 1, 1, 1, 1, 1 };
int target = 4;
List<List<int>> res = FourSum(arr, target);
foreach (List<int> quad in res) {
Console.WriteLine(string.Join(" ", quad));
}
}
}
JavaScript
function fourSum(arr, target) {
const n = arr.length;
const mp = new Map();
// Ideally we should us an unordered_set here, but JavaScript
// does not support arrays as keys in a Set
// So we have used Set to keep the code simple.
const resset = new Set();
// Store all pairs for every possible pairs sum
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const sum = arr[i] + arr[j];
if (!mp.has(sum)) {
mp.set(sum, []);
}
mp.get(sum).push([i, j]);
}
}
// Pick the first two elements
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
// Find pairs with the remaining sum
const rem = target - arr[i] - arr[j];
if (mp.has(rem)) {
const pairs = mp.get(rem);
for (const p of pairs) {
// Ensure no two indexes are same in a quaduple and
// all indexes are in sorted order
if (p[0] !== i && p[1] !== i && p[0] !== j && p[1] !== j) {
const curr = [p[0], p[1], i, j];
curr.sort((a, b) => a - b);
resset.add(curr.toString());
}
}
}
}
}
const res = Array.from(resset).map(str => str.split(',').map(Number));
return res;
}
// Driver Code
const arr = [1, 1, 1, 1, 1];
const target = 4;
const res = fourSum(arr, target);
for (const quad of res) {
console.log(quad.join(' '));
}
Each implementation maintains the structure and logic provided in your C++ example, converting it to the respective language syntax and any necessary data structure adjustments.