diff --git a/DSA_Codesheet/Python/merge_sort.py b/DSA_Codesheet/Python/merge_sort.py deleted file mode 100644 index 6eaa52f..0000000 --- a/DSA_Codesheet/Python/merge_sort.py +++ /dev/null @@ -1,53 +0,0 @@ -def merge(arr, l, m, r): - - n1 = m - l + 1 - n2 = r - m - L = [0] * n1 - R = [0] * n2 - - for i in range(0, n1): - L[i] = arr[l + i] - - for j in range(0, n2): - R[j] = arr[m + 1 + j] - - i = 0 - j = 0 - k = l - while i < n1 and j < n2: - if L[i] <= R[j]: - arr[k] = L[i] - i += 1 - else: - arr[k] = R[j] - j += 1 - k += 1 - - while i < n1: - arr[k] = L[i] - i += 1 - k += 1 - - while j < n2: - arr[k] = R[j] - j += 1 - k += 1 - - -def merge_sort(arr, l, r): - - if l < r: - m = (l + r) // 2 - merge_sort(arr, l, m) - merge_sort(arr, m + 1, r) - merge(arr, l, m, r) - - -arr = input("Enter a list of numbers: ").split(" ") - -arr = [int(i) for i in arr] - -merge_sort(arr, 0, len(arr) - 1) - -for i in arr: - print(i,end=' ') 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)