Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created February 8, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/d3be1545077391d5d428 to your computer and use it in GitHub Desktop.
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..
#!/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