You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sort(A, A+n); //sort the array, it becomes easy to take the sum.
17
+
int i, j, k, count = 0;
18
+
for(i = 0;i<=n-3;i++) //i should only run till n-3 as we need three numbers for a triplet.
19
+
{
20
+
j = i+1;
21
+
k = n-1;
22
+
while(j<k) //two-pointer appraoch is used.
23
+
{
24
+
if(A[i] + A[j] + A[k] < X) //since the array is sorted, if the sum if less than X, the we need to increment j.
25
+
{
26
+
j++;
27
+
}
28
+
elseif(A[i] + A[j] + A[k] > X) //If the sum is greater than X, we need to decrement k.
29
+
{
30
+
k--;
31
+
}
32
+
elseif(A[i] + A[j] + A[k] == X) //If the sum is equal to X we need to increement j and decrement k, so that we may find other combinations of the given sum.
0 commit comments