Given a number N and for any number K for a series formed as K, K + 1, K + 2, K + 3, K + 4, ……., K + N. The task is to find the number of unique sums that can be obtained by adding two or more integers from the series of N + 1 integers.
Examples:
Input: N = 3
Output: 10
Explanation:
The possible unique combinations are:
(K) + (K + 1) = 2*K + 1
(K) + (K + 2) = 2*K + 2
(K) + (K + 3) = 2*K + 3
(K + 1) + (K + 3) = 2*K + 4
(K + 2) + (K + 3) = 2*K + 5
(K) + (K + 1) + (K + 2) = 3*K + 3
(K) + (K + 1) + (K + 3) = 3*K + 4
(K) + (K + 2) + (K + 3) = 3*K + 5
(K + 1) + (K + 2) + (K + 3) = 3*K + 6
(K) + (K + 1) + (K + 2) + (K + 3) = 4*K + 6
So in total, there are 10 unique ways.Input: N = 4
Output: 20
Explanation:
The possible unique combinations are 20 in number.
Approach: The following observations are to be made:
- Since K is number the only significance it has is that two subsets with different sizes cannot have the same sum.
- A unique sum can be obtained from the minimum possible sum of the subset to the maximum possible sum of subset having size as X where (2 ≤ X ≤ (N + 1)).
For Example:N = 4
The Series is = {K, K + 1, K + 2, K + 3, K + 4}
For K = 2, minimum_sum = (K, K + 1) = 2*K + 1
and maximum_sum = (K + 3, K + 4) = 2*K + 7
The maximum distinct sums possible with K = 2 are (maximum_sum – minimum_sum + 1) = (7 – 1 + 1) = 7.
Follow the steps below to solve the problem:
- Initialize two arrays array fsum[] and rsum[] each of size N + 1 to 0.
- For each element of both the arrays fsum[] and rsum[], update fsum[i] with ar[i] + fsum[i – 1] and rsum[i] with ar[i] + fsum[i + 1].
- Initialize a variable ans to 1 that stores the count of different possible sums of the given series.
- For each possible subset size X, where (2 ≤ X ≤ (N + 1)), add the value 1 + rsum[n + 1 – k] + fsum[k] to ans.
- The value of ans is the required answer hence return it.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the unique sum int count_unique_sum(int n) { int i, ar[n + 1], fsum[n + 1]; int rsum[n + 1], ans = 1; // Initialize array fsum[] with 0 memset(fsum, 0, sizeof fsum); // Initialize array rsum[] with 0 memset(rsum, 0, sizeof rsum); for (i = 0; i <= n; i++) { ar[i] = i; } // Set fsum[0] as ar[0] fsum[0] = ar[0]; // Set rsum[0] as ar[n] rsum[n] = ar[n]; // For each i update fsum[i] with // ar[i] + fsum[i - 1] for (i = 1; i <= n; i++) { fsum[i] = ar[i] + fsum[i - 1]; } // For each i from n-1, update // rsum[i] with ar[i] + fsum[i + 1] for (i = n - 1; i >= 0; i--) { rsum[i] = ar[i] + rsum[i + 1]; } // K represent size of subset as // explained above for (int k = 2; k <= n; k++) { // Using above relation ans += 1 + rsum[n + 1 - k] - fsum[k - 1]; } // Return the result return ans; } // Driver Code int main() { // Given a number N int N = 4; // Function Call cout << count_unique_sum(N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to count the unique sum 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; // Initialize array fsum[] with 0 Arrays.fill(fsum, 0); // Initialize array rsum[] with 0 Arrays.fill(rsum, 0); for (i = 0; i <= n; i++) { ar[i] = i; } // Set fsum[0] as ar[0] fsum[0] = ar[0]; // Set rsum[0] as ar[n] rsum[n] = ar[n]; // For each i update fsum[i] with // ar[i] + fsum[i - 1] for (i = 1; i <= n; i++) { fsum[i] = ar[i] + fsum[i - 1]; } // For each i from n-1, update // rsum[i] with ar[i] + fsum[i + 1] for (i = n - 1; i >= 0; i--) { rsum[i] = ar[i] + rsum[i + 1]; } // K represent size of subset as // explained above for (int k = 2; k <= n; k++) { // Using above relation ans += 1 + rsum[n + 1 - k] - fsum[k - 1]; } // Return the result return ans; } // Driver Code public static void main(String[] args) { // Given a number N int N = 4; // Function Call System.out.print(count_unique_sum(N)); } } // This code is contributed by rock__cool |
Python3
# Python3 program for the above approach # Function to count the unique sum def count_unique_sum(n): ar = [0] * (n + 1) fsum = [0] * (n + 1) rsum = [0] * (n + 1) ans = 1 for i in range(0, n + 1): ar[i] = i # Set fsum[0] as ar[0] fsum[0] = ar[0] # Set rsum[0] as ar[n] rsum[n] = ar[n] # For each i update fsum[i] with # ar[i] + fsum[i - 1] for i in range(1, n + 1): fsum[i] = ar[i] + fsum[i - 1] # For each i from n-1, update # rsum[i] with ar[i] + fsum[i + 1] for i in range(n - 1, -1, -1): rsum[i] = ar[i] + rsum[i + 1] # K represent size of subset as # explained above for k in range(2, n + 1): # Using above relation ans += (1 + rsum[n + 1 - k] - fsum[k - 1]) # Return the result return ans # Driver Code # Given a number N N = 4 # Function call print(count_unique_sum(N)) # This code is contributed by sanjoy_62 |
C#
// C# program for the above approach using System; class GFG{ // Function to count the unique sum 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; for (i = 0; i <= n; i++) { ar[i] = i; } // Set fsum[0] as ar[0] fsum[0] = ar[0]; // Set rsum[0] as ar[n] rsum[n] = ar[n]; // For each i update fsum[i] with // ar[i] + fsum[i - 1] for (i = 1; i <= n; i++) { fsum[i] = ar[i] + fsum[i - 1]; } // For each i from n-1, update // rsum[i] with ar[i] + fsum[i + 1] for (i = n - 1; i >= 0; i--) { rsum[i] = ar[i] + rsum[i + 1]; } // K represent size of subset as // explained above for (int k = 2; k <= n; k++) { // Using above relation ans += 1 + rsum[n + 1 - k] - fsum[k - 1]; } // Return the result return ans; } // Driver Code public static void Main(String[] args) { // Given a number N int N = 4; // Function Call Console.Write(count_unique_sum(N)); } } // This code is contributed by PrinciRaj1992 |
20
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommended Posts:
- Subsequence pair from given Array having all unique and all same elements respectively
- Count of all unique substrings with non-repeating characters
- Count of subarrays of an Array having all unique digits
- Find sum of all unique elements in the array for K queries
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Minimum LCM and GCD possible among all possible sub-arrays
- Count of all possible pairs having sum of LCM and GCD equal to N
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Generate all unique partitions of an integer
- All pairs whose xor gives unique prime
- Print all numbers less than N with at-most 2 unique digits
- Generate all unique partitions of an integer | Set 2
- Color all boxes in line such that every M consecutive boxes are unique
- Check if all Prime factors of number N are unique or not
- Minimum length subarray containing all unique elements after Q operations
- Unique element in an array where all elements occur K times except one | Set 2
- Shortest subarray to be removed to make all Array elements unique
- Find the GCD of LCM of all unique pairs in an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


