forked from mrizky-kur/Redux-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwopointer.cpp
More file actions
42 lines (34 loc) · 770 Bytes
/
twopointer.cpp
File metadata and controls
42 lines (34 loc) · 770 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
38
39
40
41
42
// C++ Program Illustrating Naive Approach to
// Find if There is a Pair in A[0..N-1] with Given Sum
// Importing all libraries
#include <bits/stdc++.h>
using namespace std;
bool isPairSum(int A[], int N, int X)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// as equal i and j means same element
if (i == j)
continue;
// pair exists
if (A[i] + A[j] == X)
return true;
// as the array is sorted
if (A[i] + A[j] > X)
break;
}
}
// No pair found with given sum.
return false;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 5, 8, 9, 10, 11 };
int val = 17;
int arrSize = *(&arr + 1) - arr;
sort(arr, arr + arrSize); // Sort the array
// Function call
cout << isPairSum(arr, arrSize, val);
return 0;
}