Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | Java program for the above approach ; Function to count the unique sum ; Initialize array fsum [ ] with 0 ; Initialize array rsum [ ] with 0 ; 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 ; Driver Code ; Given a number N ; Function Call
import java . util . * ; class GFG { static int count_unique_sum ( int n ) { int i ; int ar [ ] = new int [ n + 1 ] ; int fsum [ ] = new int [ n + 1 ] ; int rsum [ ] = new int [ n + 1 ] ; int ans = 1 ; Arrays . fill ( fsum , 0 ) ; Arrays . fill ( rsum , 0 ) ; for ( i = 0 ; i <= n ; i ++ ) { ar [ i ] = i ; } fsum [ 0 ] = ar [ 0 ] ; rsum [ n ] = ar [ n ] ; for ( i = 1 ; i <= n ; i ++ ) { fsum [ i ] = ar [ i ] + fsum [ i - 1 ] ; } for ( i = n - 1 ; i >= 0 ; i -- ) { rsum [ i ] = ar [ i ] + rsum [ i + 1 ] ; } for ( int k = 2 ; k <= n ; k ++ ) { ans += 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . print ( count_unique_sum ( N ) ) ; } }