-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from yashashwini16/main
added triplet sum in python
- Loading branch information
Showing
2 changed files
with
28 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |