From e4906b35754686c3c5a4da8706c17fa73c71a2b0 Mon Sep 17 00:00:00 2001 From: Yashaswini B <107267601+yashashwini16@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:46:17 +0530 Subject: [PATCH 1/3] Create triplet_sum.py --- DSA_Codesheet/Python/triplet_sum.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 DSA_Codesheet/Python/triplet_sum.py diff --git a/DSA_Codesheet/Python/triplet_sum.py b/DSA_Codesheet/Python/triplet_sum.py new file mode 100644 index 0000000..533438f --- /dev/null +++ b/DSA_Codesheet/Python/triplet_sum.py @@ -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) From f73f2c219243fcb1e4424a6d16ae53b5f5da5732 Mon Sep 17 00:00:00 2001 From: Yashaswini B <107267601+yashashwini16@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:59:35 +0530 Subject: [PATCH 2/3] added merge_sort.py --- DSA_Codesheet/Python/merge_sort.py | 40 ++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 DSA_Codesheet/Python/merge_sort.py diff --git a/DSA_Codesheet/Python/merge_sort.py b/DSA_Codesheet/Python/merge_sort.py new file mode 100644 index 0000000..007dd26 --- /dev/null +++ b/DSA_Codesheet/Python/merge_sort.py @@ -0,0 +1,40 @@ +def merge_sort(arr): + if len(arr) > 1: + mid = len(arr) // 2 # Find the middle of the array + left_half = arr[:mid] # Divide the array into two halves + right_half = arr[mid:] + + merge_sort(left_half) # Recursively sort the first half + merge_sort(right_half) # Recursively sort the second half + + i = j = k = 0 + + # Merge the two halves back together + while i < len(left_half) and j < len(right_half): + if left_half[i] < right_half[j]: + arr[k] = left_half[i] + i += 1 + else: + arr[k] = right_half[j] + j += 1 + k += 1 + + while i < len(left_half): + arr[k] = left_half[i] + i += 1 + k += 1 + + while j < len(right_half): + arr[k] = right_half[j] + j += 1 + k += 1 + +# Input +n = int(input()) +arr = list(map(int, input().split())) + +# Perform Merge Sort on the array +merge_sort(arr) + +# Output the sorted array +print(" ".join(map(str, arr))) From 58e7cb6f3890944cc7c9d3294de3d1faf3d2b268 Mon Sep 17 00:00:00 2001 From: Chitresh Gyanani <129378666+Chitresh-code@users.noreply.github.com> Date: Sun, 3 Dec 2023 15:53:13 +0530 Subject: [PATCH 3/3] Delete DSA_Codesheet/Python/merge_sort.py --- DSA_Codesheet/Python/merge_sort.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 DSA_Codesheet/Python/merge_sort.py diff --git a/DSA_Codesheet/Python/merge_sort.py b/DSA_Codesheet/Python/merge_sort.py deleted file mode 100644 index e69de29..0000000