Pair with given product | Set 1 (Find if any pair exists) - GeeksforGeeks
Given an array and a number x, find if there is a pair with product equal to x.
we will be discussing approach to print all pairs with product equal to 0.
Read full article from Pair with given product | Set 1 (Find if any pair exists) - GeeksforGeeks
Given an array and a number x, find if there is a pair with product equal to x.
bool isProduct(int arr[], int n, int x){ if (n < 2) return false; // Create an empty set and insert first // element into it unordered_set<int> s; // Traverse remaining elements for (int i=0; i<n; i++) { // 0 case must be handles explicitly as // x % 0 is undefined behaviour in C++ if (arr[i] == 0) { if (x == 0) return true; else continue; } // x/arr[i] exists in hash, then we // found a pair if (x%arr[i] == 0) { if (s.find(x/arr[i]) != s.end()) return true; // Insert arr[i] s.insert(arr[i]); } } return false;}bool isProduct(int arr[], int n, int x){ // Consider all possible pairs and check for // every pair. for (int i=0; i<n-1; i++) for (int j=i+1; i<n; i++) if (arr[i] * arr[j] == x) return true; return false;}we will be discussing approach to print all pairs with product equal to 0.
Read full article from Pair with given product | Set 1 (Find if any pair exists) - GeeksforGeeks