// Testing program public static void Main(string[] args) { int[] arr = new int[] { 3, 4, 7, 1, 2, 9, 8 }; FourElements a = new FourElements(); a.findPairs_sum(arr); /* * Output: * (3, 8) and (4, 7) * Thanks to Gaurav Ahirwar for suggesting above solutions. * * 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. * * 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 < d2 * See this for solution of exercise. * http://qa.geeksforgeeks.org/3921/find-index-values-that-satisfy-where-integers-values-array */ arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; findPairs_product(arr, arr.Length); /* * Output: * 1 6 and 2 3 * 1 8 and 2 4 * 2 6 and 3 4 * 3 8 and 4 6 * Time Complexity : O(n2) assuming hash search and insert operations take O(1) time. */ }
public pair(FourElements outerInstance, int f, int s) { this.outerInstance = outerInstance; first = f; second = s; }