From a420a7fc967bce74fa9e05a8a885342e36122581 Mon Sep 17 00:00:00 2001 From: phdtrong <70780829+phdtrong@users.noreply.github.com> Date: Wed, 14 Apr 2021 10:01:53 -0700 Subject: [PATCH] Create Conditional Triplet.cpp for a Leetcode problem #21 This is for the Opening issue#21 from repo, https://github.com/sastava007/Tech-Interview-Preparation/issues/21 the corresponding Leetcode problem link 1577 as showing below: https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/ --- Leetcode/Misclenaous/ConditionalTriplets | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Leetcode/Misclenaous/ConditionalTriplets diff --git a/Leetcode/Misclenaous/ConditionalTriplets b/Leetcode/Misclenaous/ConditionalTriplets new file mode 100644 index 0000000..140339d --- /dev/null +++ b/Leetcode/Misclenaous/ConditionalTriplets @@ -0,0 +1,32 @@ +class Solution { +public: + int numTriplets(vector& nums1, vector& nums2) { + //result of the function + int resultOfTriplet = 0; + + //Count type 1 + for(unsigned int i=0; i < nums1.size(); i++){ + for(unsigned int j=0; j < nums2.size(); j++){ + for(unsigned int k=j+1; k < nums2.size(); k++){ + if((nums1[i]*nums1[i]) == nums2[j]*nums2[k]){ + resultOfTriplet++; + } + } + } + } + + //Count type 2 + for(int i=0; i < nums2.size(); i++){ + for(int j=0; j < nums1.size(); j++){ + for(int k=j+1; k < nums1.size(); k++){ + if((nums2[i]*nums2[i]) == nums1[j]*nums1[k]){ + resultOfTriplet++; + } + } + } + } + + //Return result after working + return resultOfTriplet; + } +};