Skip to content

Commit 0346f8d

Browse files
authored
created file pair with sum
1 parent bd867fc commit 0346f8d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pair-sum.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
// C++ implementation of simple method to find count of
3+
// pairs with given sum.
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
// Returns number of pairs in arr[0..n-1] with sum equal
8+
// to 'sum'
9+
int getPairsCount(int arr[], int n, int sum)
10+
{
11+
int count = 0; // Initialize result
12+
13+
// Consider all possible pairs and check their sums
14+
for (int i=0; i<n; i++)
15+
for (int j=i+1; j<n; j++)
16+
if (arr[i]+arr[j] == sum)
17+
count++;
18+
19+
return count;
20+
}
21+
22+
// Driver function to test the above function
23+
int main()
24+
{
25+
int arr[] = {1, 5, 7, -1, 5} ;
26+
int n = sizeof(arr)/sizeof(arr[0]);
27+
int sum = 6;
28+
cout << "Count of pairs is "
29+
<< getPairsCount(arr, n, sum);
30+
return 0;
31+
}

0 commit comments

Comments
 (0)