Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find number of distinct quadruple that forms target sum in python
Finding distinct quadruples from four arrays that sum to a target value is a common algorithmic problem. We can solve this efficiently using a hash map to store intermediate sums and reduce the time complexity from O(n?) to O(n²).
Problem Understanding
Given four arrays A, B, C, D and a target sum, we need to find the count of distinct quadruples (i, j, k, l) where A[i] + B[j] + C[k] + D[l] equals the target.
For example, with A = [5, 4, 3], B = [8, 4], C = [6, 2], D = [4, 10], target = 23, the valid quadruples are:
- [5, 8, 6, 4] ? 5 + 8 + 6 + 4 = 23
- [3, 4, 6, 10] ? 3 + 4 + 6 + 10 = 23
- [3, 8, 2, 10] ? 3 + 8 + 2 + 10 = 23
Algorithm
The approach uses a two-phase strategy:
- Phase 1: Store all possible sums of A[i] + B[j] in a hash map
- Phase 2: For each C[k] + D[l], check if (target - (C[k] + D[l])) exists in the map
Implementation
from collections import defaultdict
def find_quadruple_count(A, B, C, D, target):
count = 0
sum_map = defaultdict(int)
# Phase 1: Store all A[i] + B[j] sums
for i in A:
for j in B:
sum_map[i + j] += 1
# Phase 2: Check for complementary sums
for k in C:
for z in D:
complement = target - (k + z)
if complement in sum_map:
count += sum_map[complement]
return count
# Example usage
A = [5, 4, 3]
B = [8, 4]
C = [6, 2]
D = [4, 10]
target = 23
result = find_quadruple_count(A, B, C, D, target)
print(f"Number of distinct quadruples: {result}")
Number of distinct quadruples: 3
How It Works
Let's trace through the algorithm:
from collections import defaultdict
A = [5, 4, 3]
B = [8, 4]
C = [6, 2]
D = [4, 10]
target = 23
sum_map = defaultdict(int)
# Phase 1: Building the sum map
print("Phase 1 - Building sum map from A and B:")
for i in A:
for j in B:
sum_map[i + j] += 1
print(f"A[{A.index(i)}] + B[{B.index(j)}] = {i} + {j} = {i + j}")
print(f"Sum map: {dict(sum_map)}")
print()
# Phase 2: Finding complements
print("Phase 2 - Finding complements from C and D:")
count = 0
for k in C:
for z in D:
complement = target - (k + z)
print(f"C[{C.index(k)}] + D[{D.index(z)}] = {k} + {z} = {k + z}")
print(f"Looking for complement: {target} - {k + z} = {complement}")
if complement in sum_map:
count += sum_map[complement]
print(f"Found! Adding {sum_map[complement]} to count")
print()
print(f"Total quadruples: {count}")
Phase 1 - Building sum map from A and B:
A[0] + B[0] = 5 + 8 = 13
A[0] + B[1] = 5 + 4 = 9
A[1] + B[0] = 4 + 8 = 12
A[1] + B[1] = 4 + 4 = 8
A[2] + B[0] = 3 + 8 = 11
A[2] + B[1] = 3 + 4 = 7
Sum map: {13: 1, 9: 1, 12: 1, 8: 1, 11: 1, 7: 1}
Phase 2 - Finding complements from C and D:
C[0] + D[0] = 6 + 4 = 10
Looking for complement: 23 - 10 = 13
Found! Adding 1 to count
C[0] + D[1] = 6 + 10 = 16
Looking for complement: 23 - 16 = 7
Found! Adding 1 to count
C[1] + D[0] = 2 + 4 = 6
Looking for complement: 23 - 6 = 17
C[1] + D[1] = 2 + 10 = 12
Looking for complement: 23 - 12 = 11
Found! Adding 1 to count
Total quadruples: 3
Time Complexity
The algorithm has O(n²) time complexity where n is the average length of the arrays, which is much better than the brute force O(n?) approach.
Conclusion
This hash map approach efficiently finds quadruple counts by splitting the problem into two phases. The key insight is storing A+B sums and looking for complements with C+D sums, achieving optimal O(n²) complexity.
