Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6065922

Browse files
Triplet sum edit
1 parent 80a6456 commit 6065922

File tree

1 file changed

+119
-1
lines changed

1 file changed

+119
-1
lines changed

‎Scripts/Triplet sum.py‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,123 @@ def triplet_sum(array: list, req_sum: int):
6969
"""
7070

7171
'''
72-
<GeeksforGeeks page content>
72+
Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false. For example, if the given array is {12, 3, 4, 1, 6, 9} and given sum is 24, then there is a triplet (12, 3 and 9) present in array whose sum is 24.
73+
74+
Recommended: Please solve it on "PRACTICE " first, before moving on to the solution.
75+
Method 1 (Naive)
76+
A simple method is to generate all possible triplets and compare the sum of every triplet with the given value. The following code implements this simple method using three nested loops.
77+
CJava
78+
# include <stdio.h>
79+
80+
// returns true if there is triplet with sum equal
81+
// to 'sum' present in A[]. Also, prints the triplet
82+
bool find3Numbers(int A[], int arr_size, int sum)
83+
{
84+
int l, r;
85+
86+
// Fix the first element as A[i]
87+
for (int i = 0; i < arr_size-2; i++)
88+
{
89+
// Fix the second element as A[j]
90+
for (int j = i+1; j < arr_size-1; j++)
91+
{
92+
// Now look for the third number
93+
for (int k = j+1; k < arr_size; k++)
94+
{
95+
if (A[i] + A[j] + A[k] == sum)
96+
{
97+
printf("Triplet is %d, %d, %d", A[i], A[j], A[k]);
98+
return true;
99+
}
100+
}
101+
}
102+
}
103+
104+
// If we reach here, then no triplet was found
105+
return false;
106+
}
107+
108+
/* Driver program to test above function */
109+
int main()
110+
{
111+
int A[] = {1, 4, 45, 6, 10, 8};
112+
int sum = 22;
113+
int arr_size = sizeof(A)/sizeof(A[0]);
114+
115+
find3Numbers(A, arr_size, sum);
116+
117+
return 0;
118+
}
119+
Run on IDE
120+
121+
Output:
122+
Triplet is 4, 10, 8
123+
Time Complexity: O(n^3)
124+
125+
126+
127+
Method 2 (Use Sorting)
128+
Time complexity of the method 1 is O(n^3). The complexity can be reduced to O(n^2) by sorting the array first, and then using method 1 of this post in a loop.
129+
1) Sort the input array.
130+
2) Fix the first element as A[i] where i is from 0 to array size – 2. After fixing the first element of triplet, find the other two elements using method 1 of this post.
131+
C++Java
132+
// C++ program to find a triplet
133+
# include <bits/stdc++.h>
134+
using namespace std;
135+
136+
// returns true if there is triplet with sum equal
137+
// to 'sum' present in A[]. Also, prints the triplet
138+
bool find3Numbers(int A[], int arr_size, int sum)
139+
{
140+
int l, r;
141+
142+
/* Sort the elements */
143+
sort(A, A+arr_size);
144+
145+
/* Now fix the first element one by one and find the
146+
other two elements */
147+
for (int i=0; i<arr_size-2; i++)
148+
{
149+
150+
// To find the other two elements, start two index
151+
// variables from two corners of the array and move
152+
// them toward each other
153+
l = i + 1; // index of the first element in the
154+
// remaining elements
155+
r = arr_size-1; // index of the last element
156+
while (l < r)
157+
{
158+
if( A[i] + A[l] + A[r] == sum)
159+
{
160+
printf("Triplet is %d, %d, %d", A[i],
161+
A[l], A[r]);
162+
return true;
163+
}
164+
else if (A[i] + A[l] + A[r] < sum)
165+
l++;
166+
else // A[i] + A[l] + A[r] > sum
167+
r--;
168+
}
169+
}
170+
171+
// If we reach here, then no triplet was found
172+
return false;
173+
}
174+
175+
/* Driver program to test above function */
176+
int main()
177+
{
178+
int A[] = {1, 4, 45, 6, 10, 8};
179+
int sum = 22;
180+
int arr_size = sizeof(A)/sizeof(A[0]);
181+
182+
find3Numbers(A, arr_size, sum);
183+
184+
return 0;
185+
}
186+
Run on IDE
187+
188+
Output:
189+
Triplet is 4, 8, 10
190+
Time Complexity: O(n^2)
73191
'''

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /