Count the size of $|\{(b_1, \dots, b_n): (-1)^{b_1} a_1 + \cdots + (-1)^{b_n} a_n =0, b_i \in \{0,1\} \}|$ in $O(n)$ way What is the size of $|\{(b_1, \dots, b_n): (-1)^{b_1} a_1 + \cdots + (-1)^{b_n} a_n =0, b_i \in \{0,1\} \}|$ when all $a_i \in \mathbb{Z}^+$ are given, fixed positive integers ? How can you count this size in a $O(n)$ way (especially by computer algorithm)? What is a main trick to count this in a fast way? Is there any textbook or topics that deals with such things?
 A: Let us rewrite the main condition as 
$$
\tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = \sum\limits_{i: b_i = 1} a_i - \sum\limits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of $\{1,...,n\}$  which partition $\{a_1,...,a_n\}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums. 
def partition(a):
    # a is the array in question
    # we decide whether there is a partition of a into equal sum subsets

    num = sum(a)
    if num%2 != 0:
        return False

    num = num//2
    n = len(a)

    memo = (num+1)*[None]       # will be used for memoization, see below
    for i in range(num + 1):
        memo[i] = (n+1)*[False]

    # memo[i][j] shows if the integer i can be represented by elements of a up to and including j

    for i in range(n+1):
        memo[0][i] = True # 0 can be represented by not choosing anything, the empty set

    for i in range(1, num + 1):
        for j in range(1, n + 1):
            memo[i][j] =  memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )  
            # either integer i can be represented by elements a_0, ..., a_{j-1}
            # or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1} 

    return memo[-1][-1] # the last element of the last row

The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet. 
