forked from jaisinha/DSA-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPair Target Sum Problem.cpp
59 lines (49 loc) · 1.23 KB
/
Pair Target Sum Problem.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
//C++ program to demonstrate the two pointer approach
/*Pair Target Sum Problem
Find whether there exist 2 numbers that sum to reqsum.
Important: The Array should be sorted for two pointer approach.
Idea: keep a low and high pointer, decide which pointer to move on the basis of
arr[low] + arr[high].
Time Complexity: O(N)
Space Complexity: O(1)*/
#include <bits/stdc++.h>
using namespace std;
//function
bool pairsum(int arr[], int n, int reqsum)
{
int low=0;
int high = n-1;
while(low<high)
{
if(arr[low]+arr[high] == reqsum)
{
cout<<"Index of the Elements eligible : "; //prinitng the indexes eligible
cout<<low<<" "<<high<<endl;
return true;
}
else if(arr[low] + arr[high]> reqsum) // checking the desired condition
{
high--;
}
else
{
low++;
}
}
return false;
}
// Driver Code
int main()
{
int n,reqsum;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i]; //entering the array
}
cin>>reqsum; //required sum to be checked
cout<<"1 if True & 0 if False: ";
cout<<pairsum(arr,n,reqsum)<<endl; //prinitng the desired result
return 0;
}