From 36d7a05184900b9cd6492d66b18d306ddc89a8b1 Mon Sep 17 00:00:00 2001 From: yogeshwalecha <62990948+yogeshwalecha@users.noreply.github.com> Date: Sat, 2 Oct 2021 16:28:00 +0530 Subject: [PATCH] Create CountTriplets.cpp --- DSA/Cpp/CountTriplets.cpp | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 DSA/Cpp/CountTriplets.cpp diff --git a/DSA/Cpp/CountTriplets.cpp b/DSA/Cpp/CountTriplets.cpp new file mode 100644 index 0000000..edf4287 --- /dev/null +++ b/DSA/Cpp/CountTriplets.cpp @@ -0,0 +1,55 @@ + +// C++ program to count Triplets such that at +// least one of the numbers can be written +// as sum of the other two +#include +using namespace std; + + // Function to count the number of ways to choose the triples.. + // + int countWays(int arr[], int n) + { + // compute the max value in the array and create frequency array of size max_val + 1. + + // We can also use HashMap to store frequencies. We have used an array to keep remaining code simple. + int max_val = 0; + for (int i = 0; i < n; i++) + max_val = max(max_val, arr[i]); + int freq[max_val + 1]={0}; + for (int i = 0; i < n; i++) + freq[arr[i]]++; + + int ans = 0; // stores the number of ways + + // Case 1: 0, 0, 0 + ans += freq[0] * (freq[0] - 1) * (freq[0] - 2) / 6; + + // Case 2: 0, x, x + for (int i = 1; i <= max_val; i++) + ans += freq[0] * freq[i] * (freq[i] - 1) / 2; + + // Case 3: x, x, 2*x + for (int i = 1; 2 * i <= max_val; i++) + ans += freq[i] * (freq[i] - 1) / 2 * freq[2 * i]; + + // Case 4: x, y, x + y + // iterate through all pairs (x, y) + for (int i = 1; i <= max_val; i++) { + for (int j = i + 1; i + j <= max_val; j++) + ans += freq[i] * freq[j] * freq[i + j]; + } + + return ans; + } + + int main() + { + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + } + cout<<(countWays(arr, n)); + return 0; + }