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
Find three element from different three arrays such that that a + b + c = sum in Python
Finding three elements from different arrays that sum to a target value is a common programming problem. We need to check if there exist elements a, b, c from arrays A, B, C respectively such that a + b + c equals the given sum.
So, if the input is like A = [2,3,4,5,6], B = [3,4,7,2,3], C = [4,3,5,6,7], sum = 12, then the output will be True as 4+2+6 = 12, where 4, 2, 6 are taken from A, B, C respectively.
Method 1: Brute Force Approach
The straightforward approach uses three nested loops to check all possible combinations ?
Algorithm
Iterate through each element in array A
For each element in A, iterate through array B
For each pair from A and B, iterate through array C
Check if the sum of three elements equals the target
Return True if found, False otherwise
Example
def is_sum_from_three_arr(A, B, C, target_sum):
for i in range(len(A)):
for j in range(len(B)):
for k in range(len(C)):
if (A[i] + B[j] + C[k] == target_sum):
return True
return False
A = [2, 3, 4, 5, 6]
B = [3, 4, 7, 2, 3]
C = [4, 3, 5, 6, 7]
target_sum = 12
result = is_sum_from_three_arr(A, B, C, target_sum)
print(f"Can find three elements that sum to {target_sum}: {result}")
Can find three elements that sum to 12: True
Method 2: Optimized Approach Using Set
We can optimize by using a set to store elements from the third array for faster lookup ?
def is_sum_from_three_arr_optimized(A, B, C, target_sum):
# Convert C to set for O(1) lookup
set_C = set(C)
for a in A:
for b in B:
required = target_sum - a - b
if required in set_C:
return True
return False
A = [2, 3, 4, 5, 6]
B = [3, 4, 7, 2, 3]
C = [4, 3, 5, 6, 7]
target_sum = 12
result = is_sum_from_three_arr_optimized(A, B, C, target_sum)
print(f"Optimized approach result: {result}")
Optimized approach result: True
Method 3: Finding Actual Elements
To return the actual triplet instead of just True/False ?
def find_triplet_with_sum(A, B, C, target_sum):
for a in A:
for b in B:
for c in C:
if a + b + c == target_sum:
return (a, b, c)
return None
A = [2, 3, 4, 5, 6]
B = [3, 4, 7, 2, 3]
C = [4, 3, 5, 6, 7]
target_sum = 12
triplet = find_triplet_with_sum(A, B, C, target_sum)
if triplet:
print(f"Found triplet: {triplet[0]} + {triplet[1]} + {triplet[2]} = {target_sum}")
else:
print("No triplet found")
Found triplet: 2 + 4 + 6 = 12
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n³) | O(1) | Small arrays |
| Using Set | O(n²) | O(n) | Larger arrays |
| Return Triplet | O(n³) | O(1) | When actual values needed |
Conclusion
The brute force approach is simple but has O(n³) complexity. The optimized set-based approach reduces time complexity to O(n²) by eliminating one loop. Choose the method based on your array sizes and whether you need the actual triplet values.
