-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTriplet_Sum_in_Array.cpp
69 lines (58 loc) · 1.64 KB
/
Triplet_Sum_in_Array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Problem Statement:
-----------------
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X.
Example 1:
---------
Input:
n = 6, X = 13
arr[] = [1 4 45 6 10 8]
Output:
1
Explanation: The triplet {1, 4, 8} in the array sums up to 13.
Example 2:
---------
Input:
n = 5, X = 10
arr[] = [1 2 4 3 6]
Output:
1
Explanation: The triplet {1, 3, 6} in the array sums up to 10.
Your Task: You don't need to read input or print anything. Your task is to complete the function find3Numbers()
which takes the array arr[], the size of the array (n) and the sum (X) as inputs and returns True if there exists a triplet
in the array arr[] which sums up to X and False otherwise.
Expected Time Complexity: O(n2)
Expected Auxiliary Space: O(1)
*/
// Link --> https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1#
// Code:
class Solution
{
public:
//Function to find if there exists a triplet in the
//array A[] which sums up to X.
bool find3Numbers(int a[] , int n , int X)
{
bool answer = false;
sort(a , a+n);
for(int i=0 ; i<n-2 ; i++)
{
int start = i+1;
int end = n-1;
while(start < end)
{
int temp = a[start] + a[end];
if(temp + a[i] == X)
{
answer = true;
break;
}
else if(temp > X-a[i])
end--;
else
start++;
}
}
return answer;
}
};