Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | Function to count the unique sum ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Given a number N ; Function call
def count_unique_sum ( n ) : NEW_LINE INDENT ar = [ 0 ] * ( n + 1 ) NEW_LINE fsum = [ 0 ] * ( n + 1 ) NEW_LINE rsum = [ 0 ] * ( n + 1 ) NEW_LINE ans = 1 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT ar [ i ] = i NEW_LINE DEDENT fsum [ 0 ] = ar [ 0 ] NEW_LINE rsum [ n ] = ar [ n ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fsum [ i ] = ar [ i ] + fsum [ i - 1 ] NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT rsum [ i ] = ar [ i ] + rsum [ i + 1 ] NEW_LINE DEDENT for k in range ( 2 , n + 1 ) : NEW_LINE INDENT ans += ( 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 4 NEW_LINE print ( count_unique_sum ( N ) ) NEW_LINE