Split an Array into all possible Combinations of Subsets of length 2 -
i have array, say
ar = [1,2,3,4,5]
i need find list of arrays (or matrix) contains possible ways split array exhaustive sets of pairs.
eg.
[ [[1,2],[3,4],[5]], [[1,2],[3,5],[4]], [[1,2],[4,5],[3]], [[1,3],[2,4],[5]] ... [[1,5],[3,4],[2]] ]
please try use pseudocode, or refrain using language-specific functions
i give intuition solve type of question. first can see problem can solved recursively. why that?
first take 2 elements array,
then solve same problem rest of elements of array.
if length of array 2 or 1 stop.
following rough pseudo code.
solve(arr){ if len(arr)==1 or 2{ return arr } take 2 elements (say [1, 5]) remainder = [2, 3, 4] solve(remainder) }
Comments
Post a Comment