-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpair_with_given_sum.cpp
More file actions
39 lines (37 loc) · 853 Bytes
/
pair_with_given_sum.cpp
File metadata and controls
39 lines (37 loc) · 853 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
#include <bits/stdc++.h>
using namespace std;
bool hasArrayTwoCandidates(int A[], int N, int s)
{
int l, r;
// Sorting the array
sort(A, A + N);
l = 0;
r = N-1;
while (l < r)
{
if(A[l] + A[r] == s)
{
cout<<"The suitable pair is ("<<A[l]<<","<<A[r]<<")";
return true;
}
else if(A[l] + A[r] < s)
l++;
else
r--;
}
return false;
}
int main()
{
int A[200], N, i, s;
cout<<"Enter total no. of elements to be sorted: ";
cin>>N;
cout<<"Enter the elements of array: "<<endl;
for(i=0;i<N;i++)
cin>>A[i];
cout<<"Enter the value of sum to be find: ";
cin>>s;
if (!hasArrayTwoCandidates(A, N, s))
cout << "No such pair present in the array whose sum is equal to entered sum";
return 0;
}