N is up to 20. Which means there are total of 2^20 ways of forming 2 different piles. The brute force solution will be enough but we need to find the best way to implement it. We can do it with bitmasks. In other words, we take numbers from 1 to 2^20 and get their binary representation (which must have a length of N, if it is not long enough we add zeroes to it). Then in our binary representation , the Ith position will tell us to which pile do we take our Ith elemenr. If it is 1 than we need to take it to the first pile otherwise to the second pile.
No comments:
Post a Comment