fix 12,22,12 edge case

This commit is contained in:
2025-04-02 01:14:55 +03:00
parent d683ff8165
commit e9921debf5
3 changed files with 17 additions and 2 deletions

1
inputs/input_2_3_3_2.txt Normal file
View File

@@ -0,0 +1 @@
2,3,3,2

View File

@@ -71,9 +71,16 @@ def print_pairs_with_same_sum(arr: List[int]) -> None:
# Time Complexity (O(n^2)), as we have n(n-1)/2 for the second loop.
for j in range(i + 1, n):
current_sum = arr[i] + arr[j]
pair = tuple((arr[i], arr[j]))
if pair not in seen_pairs:
# In order to follow the provided output, I must not sort the values of the pair (eg. ( 12, 21) ( 22, 11) have sum : 33)
# however in case of [12, 21, 12] this will output (12,21) (21,12), since we have a second 12. If this is not
# to be considered unique pair, then we must reverse the tuple and check if it really is unique.
pair = tuple((arr[i], arr[j]))
reversed_pair = tuple((arr[j], arr[i]))
# The check for reversed pairs.
if pair not in seen_pairs and reversed_pair not in seen_pairs:
# This will output nothing for [2,2,2,2]. Depending on the requirements we can store the pair using indices,
# then the result would be:
# (A[0],A[1]) (A[0],A[2]) (A[0],A[3]) (A[1],A[2]) (A[1],A[3]) (A[2],A[3]) - sum: 4

View File

@@ -41,6 +41,13 @@ def test_no_valid_pairs():
result = capture_output(print_pairs_with_same_sum, arr)
assert result == expected_output
# Test case: Array with no unique pairs
def test_no_uniqe_pairs():
arr = [2, 3, 3, 2]
expected_output = ""
result = capture_output(print_pairs_with_same_sum, arr)
assert result == expected_output
# Test case: Array with exactly one element
def test_single_element():
arr = [10]