please check out this: algorithm - How to find the biggest subset of an array given some constraints? - Stack Overflow