Related: Leetcode 18 - 4Sum
Find four elements a, b, c and d in an array such that a+b = c+d - GeeksforGeeks
Given an array of distinct integers, find if there are two pairs (a, b) and (c, d) such that a+b = c+d, and a, b, c and d are distinct elements. If there are multiple answers, then print any of them.
Java code:
Count all Quadruples from four arrays such that their XOR equals to 'x' - GeeksforGeeks
Given four arrays and an integer x, find the number of quadruples which satisfy a^b^c^d = x, where a belongs from Arr1, b belongs from Arr2, c belongs from Arr3, d belongs from Arr4.
The idea is to use meet in the middle algorithm.
http://qa.geeksforgeeks.org/3921/find-index-values-that-satisfy-where-integers-values-array
Read full article from Find four elements a, b, c and d in an array such that a+b = c+d - GeeksforGeeks
Find four elements a, b, c and d in an array such that a+b = c+d - GeeksforGeeks
Given an array of distinct integers, find if there are two pairs (a, b) and (c, d) such that a+b = c+d, and a, b, c and d are distinct elements. If there are multiple answers, then print any of them.
A Simple Solution is to run four loops to generate all possible quadruples of array element. For every quadruple (a, b, c, d), check if (a+b) = (c+d). Time complexity of this solution is O(n4).
An Efficient Solution can solve this problem in O(n2) time. The idea is to use hashing. We use sum as key and pair as value in hash table.
boolean findPairs(int arr[]) { // Create an empty Hash to store mapping from sum to // pair indexes HashMap<Integer,pair> map = new HashMap<Integer,pair>(); int n=arr.length; // Traverse through all possible pairs of arr[] for (int i=0; i<n; ++i) { for (int j=i+1; j<n; ++j) { // If sum of current pair is not in hash, // then store it and continue to next pair int sum = arr[i]+arr[j]; if (!map.containsKey(sum)) map.put(sum,new pair(i,j)); else // Else (Sum already present in hash) { // Find previous pair pair p = map.get(sum); // Since array elements are distinct, we don't // need to check if any element is common among pairs System.out.println("("+arr[p.first]+", "+arr[p.second]+ ") and ("+arr[i]+", "+arr[j]+")"); return true; } } } return false; }Java code:
class ArrayElements{ // Class to represent a pair class pair { int first, second; pair(int f,int s) { first = f; second = s; } }; boolean findPairs(int arr[]) { // Create an empty Hash to store mapping from sum to // pair indexes HashMap<Integer,pair> map = new HashMap<Integer,pair>(); int n=arr.length; // Traverse through all possible pairs of arr[] for (int i=0; i<n; ++i) { for (int j=i+1; j<n; ++j) { // If sum of current pair is not in hash, // then store it and continue to next pair int sum = arr[i]+arr[j]; if (!map.containsKey(sum)) map.put(sum,new pair(i,j)); else // Else (Sum already present in hash) { // Find previous pair pair p = map.get(sum); // Since array elements are distinct, we don't // need to check if any element is common among pairs System.out.println("("+arr[p.first]+", "+arr[p.second]+ ") and ("+arr[i]+", "+arr[j]+")"); return true; } } } return false; }
Exercise:
1) Extend the above solution with duplicates allowed in array.
2) Further extend the solution to print all quadruples in output instead of just one. And all quadruples should be printed printed in lexicographical order (smaller values before greater ones). Assume we have two solutions S1 and S2.
1) Extend the above solution with duplicates allowed in array.
2) Further extend the solution to print all quadruples in output instead of just one. And all quadruples should be printed printed in lexicographical order (smaller values before greater ones). Assume we have two solutions S1 and S2.
S1 : a1 b1 c1 d1 ( these are values of indices int the array ) S2 : a2 b2 c2 d2 S1 is lexicographically smaller than S2 iff a1 < a2 OR a1 = a2 AND b1 < b2 OR a1 = a2 AND b1 = b2 AND c1 < c2 OR a1 = a2 AND b1 = b2 AND c1 = c2 AND d1 < d2Related: http://massivealgorithms.blogspot.com/2015/10/find-index-of-values-that-satisfy-b-c-d.html
Given an array of distinct integers, the task is to find two pairs (a, b) and (c, d) such that ab = cd, where a, b, c and d are distinct elements.
1. For i=0 to n-1
2. For j=i+1 to n-1
a) Find prod = arr[i]*arr[j]
b) If prod is not available in hash then make
H[prod] = make_pair(i, j) // H is hash table
c) If product is also available in hash
then print previous and current elements
of array
void findPairs(int arr[], int n){ bool found = false; unordered_map<int, pair < int, int > > H; for (int i=0; i<n; i++) { for (int j=i+1; j<n; j++) { // If product of pair is not in hash table, // then store it int prod = arr[i]*arr[j]; if (H.find(prod) == H.end()) H[prod] = make_pair(i,j); // If product of pair is also available in // then print current and previous pair else { pair<int,int> pp = H[prod]; cout << arr[pp.first] << " " << arr[pp.second] << " and " << arr[i]<<" "<<arr[j]<<endl; found = true; } } } // If no pair find then print not found if (found == false) cout << "No pairs Found" << endl;}Count all Quadruples from four arrays such that their XOR equals to 'x' - GeeksforGeeks
Given four arrays and an integer x, find the number of quadruples which satisfy a^b^c^d = x, where a belongs from Arr1, b belongs from Arr2, c belongs from Arr3, d belongs from Arr4.
The idea is to use meet in the middle algorithm.
For this, observe the pattern below:
a ^ b ^ c ^ d = x
XOR c and d both sides
a ^ b ^ c ^ d ^ c ^ d = x ^ c ^ d
a ^ b ^ c ^ d ^ c ^ d = x ^ c ^ d
Since, c ^ c = 0 and d ^ d = 0
a ^ b ^ 0 ^ 0 = x ^ c ^ d
That is, a ^ b = x ^ c ^ d
Now, we just have to compute a ^ b and x ^ c ^ d which can be computed in O(n2) each and then find elements by using binary search.
Time Complexity: O(n2log(n))
Auxiliary Space: O(n2)
Auxiliary Space: O(n2)
int findQuadruples(int a[], int b[], int c[], int d[], int x, int n){ int count = 0; vector<int> v1, v2; // Loop to get two different subsets for (int i = 0 ; i < n ; i++) { for (int j = 0 ; j < n ; j++) { // v1 for first and second array v1.push_back(a[i]^b[j]); // v2 for third and forth array. // x is a constant, so no need for // a separate loop v2.push_back(x ^ c[i] ^ d[j]); } } // Sorting the first set (Containing XOR // of a[] and b[] sort(v1.begin(), v1.end()); // Finding the lower and upper bound of an // element to find its number for (int i = 0 ; i < v2.size() ; i++) { // Count number of occurrences of v2[i] in sorted // v1[] and add the count to result. auto low = lower_bound(v1.begin(), v1.end(), v2[i]); auto high = upper_bound(v1.begin(), v1.end(), v2[i]); count += high - low; } return count;}http://qa.geeksforgeeks.org/3921/find-index-values-that-satisfy-where-integers-values-array
1) Extend the above solution with duplicates allowed in array.
2) Further extend the solution to print all quadruples in output instead of just one. And all quadruples should be printed printed in lexicographical order (smaller values before greater ones). Assume we have two solutions S1 and S2.
2) Further extend the solution to print all quadruples in output instead of just one. And all quadruples should be printed printed in lexicographical order (smaller values before greater ones). Assume we have two solutions S1 and S2.
S1 : a1 b1 c1 d1 ( these are values of indices int the array ) S2 : a2 b2 c2 d2 S1 is lexicographically smaller than S2 iff a1 < a2 OR a1 = a2 AND b1 < b2 OR a1 = a2 AND b1 = b2 AND c1 < c2 OR a1 = a2 AND b1 = b2 AND c1 = c2 AND d1 < d2https://github.com/nagajyothi/InterviewBit/blob/master/Hashing/Equal.java
Read full article from Find four elements a, b, c and d in an array such that a+b = c+d - GeeksforGeeks