forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2sum.cpp
More file actions
37 lines (30 loc) · 614 Bytes
/
2sum.cpp
File metadata and controls
37 lines (30 loc) · 614 Bytes
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
// c++ Program Check if a pair exists with given sum in given array
#include <bits/stdc++.h>
using namespace std;
// Function to find and print pair
bool chkPair(int A[], int size, int x)
{
for (int i = 0; i < (size - 1); i++) {
for (int j = (i + 1); j < size; j++) {
if (A[i] + A[j] == x) {
return 1;
}
}
}
return 0;
}
// Driver code
int main()
{
int A[] = { 0, -1, 2, -3, 1 };
int x = -2;
int size = sizeof(A) / sizeof(A[0]);
if (chkPair(A, size, x)) {
cout << "Yes" << endl;
}
else {
cout << "No" << x << endl;
}
return 0;
}
// This code is contributed by Mayur Shrivastava