From 59e8d6d577ec495348505e725096038e1a12bb04 Mon Sep 17 00:00:00 2001 From: Tusharr0305 <113424554+Tusharr0305@users.noreply.github.com> Date: Sat, 29 Oct 2022 11:28:13 +0530 Subject: [PATCH] Create count_pair_with_given_sum.cpp Added Subarray problem --- .../Algorithms/count_pair_with_given_sum.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 LeetcodeProblems/Algorithms/count_pair_with_given_sum.cpp diff --git a/LeetcodeProblems/Algorithms/count_pair_with_given_sum.cpp b/LeetcodeProblems/Algorithms/count_pair_with_given_sum.cpp new file mode 100644 index 0000000..a0ddd2b --- /dev/null +++ b/LeetcodeProblems/Algorithms/count_pair_with_given_sum.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +int getPairsCount(int arr[], int n, int sum) +{ + int count = 0; // Initialize result + + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + if (arr[i] + arr[j] == sum) + count++; + + return count; +} + +int main() +{ + int arr[] = { 1, 5, 7, -1, 5 }; + int n = sizeof(arr) / sizeof(arr[0]); + int sum = 6; + cout << "Count of pairs is " + << getPairsCount(arr, n, sum); + return 0; +} +