Created
February 8, 2015 16:18
-
-
Save zsrinivas/d3be1545077391d5d428 to your computer and use it in GitHub Desktop.
count the number of ways to partition an array into two equal sets..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
# pylint: disable=C0111 | |
def solve(arr): | |
fhalf = set() | |
prefl, i = 0, 0 | |
for x in xrange(len(arr)): | |
fhalf.add(arr[x]) | |
if len(fhalf) != prefl: | |
prefl = len(fhalf) | |
i = x | |
w = i | |
shalf = set() | |
for x in xrange(len(arr) - 1, i, -1): | |
shalf.add(arr[x]) | |
if len(shalf) == prefl: | |
w = x | |
break | |
return max(w - i, 0) | |
def main(): | |
print solve([1, 5, 1]) | |
print solve([4, 1, 1, 2, 4, 2, 4, 1]) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment