Skip to content

Commit

Permalink
Merge pull request #95 from yashashwini16/main
Browse files Browse the repository at this point in the history
added triplet sum in python
  • Loading branch information
Chitresh-code authored Dec 3, 2023
2 parents f787a75 + 58e7cb6 commit 0dc550b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 53 deletions.
53 changes: 0 additions & 53 deletions DSA_Codesheet/Python/merge_sort.py

This file was deleted.

28 changes: 28 additions & 0 deletions DSA_Codesheet/Python/triplet_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def find_triplet_sum(arr, n, X):
# Sort the input array
arr.sort()

for i in range(n - 2):
left = i + 1
right = n - 1

while left < right:
current_sum = arr[i] + arr[left] + arr[right]

if current_sum == X:
return 1 # Triplet found

if current_sum < X:
left += 1
else:
right -= 1

return 0 # No triplet found

# Input
n, X = map(int, input().split())
arr = list(map(int, input().split()))

# Check if a triplet with the given sum X exists in the array
result = find_triplet_sum(arr, n, X)
print(result)

0 comments on commit 0dc550b

Please sign in to comment.