Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function findArrayQuadCombination(arr, S):
- if (arr == null OR S == null):
- return null
- n = length(arr)
- if (n < 4):
- return null
- # hashing implementation language dependent:
- pairHash = new HashTable()
- for i from 0 to n-1
- for j from i+1 to n-1
- if !pairHash.isMapped(arr[i]+arr[j]):
- pairHash.map(arr[i]+arr[j], [])
- pairHash.get(arr[i]+arr[j]).push([i, j])
- for pairSum in pairHash.getKeys()
- if pairHash.isMapped(S - pairSum):
- pairsA = pairHash.get(pairSum)
- pairsB = pairHash.get(S - pairsSum)
- combination = find4Uniques(pairsA, pairsB)
- if (combination != null):
- return combination
- return null
- # Helper function.
- # Gets 2 arrays of sub-arrays of 2 numbers
- # Gets 4 unique numbers, from 2 sub-arrays of different arrays
- function find4Uniques(A, B):
- lenA = length(A)
- lenB = length(B)
- for i from 0 to lenA-1:
- for j from 0 to lenB-1:
- if ( A[i][0] == B[j][0] OR A[i][1] == B[j][1] OR
- A[i][0] == B[j][1] OR A[i][1] == B[j][0] ):
- continue
- else:
- return [A[i][0], A[i][1], B[j][0], B[j][1]]
- return null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement