Given three positive integers S, K, and N, the task is to find K distinct positive integers, not exceeding N and having sum equal to S. If it is not possible to find K such positive integers, print -1.
Examples:
Input: S = 15, K = 4, N = 8
Output: 1 2 4 8
Explanation:
One possible set of K such numbers is {1, 2, 3, 4} ( Since, 1 + 2 + 4 + 8 =15).
Other possible set of K numbers are {2, 3, 4, 6}, {1, 3, 4, 7}, etc.Input: S = 14, K = 5, N = 6
Output: -1
Approach: Follow the steps below to solve the problem:
- If N is less than K, then print -1.
- If S is less than the sum of first K natural numbers, i.e. (1 + 2 + … + K) or if S is greater than the sum of last K natural numbers, i.e. from N to N – K + 1, then print -1.
- Iterate from 1 and keep adding every natural number encountered in a variable, say s1, while s1 ≤ S. Insert all the encountered elements in an array, say nums[].
- Extract K – 1 elements from nums[] and store in another array, say answer.
- The Kth element in the array answer[] will be (s1 – s2), where s2 is the sum of the K – 1 elements present in the array answer[].
- Traverse the array answer[] in reverse and reduce all array elements to less than or equal to N.
Below is the implementation of the above approach:
C++
// C++ implementation// for the above approach #include <bits/stdc++.h>using namespace std; // Function to represent S as// the sum of K positive integers// less than or equal to Nvoid solve(int S, int K, int N){ if (K > N) { cout << "-1" << endl; return; } int max_sum = 0, min_sum = 0; for (int i = 1; i <= K; i++) { min_sum += i; max_sum += N - i + 1; } // If S can cannot be represented // as sum of K integers if (S < min_sum || S > max_sum) { cout << "-1" << endl; return; } int s1 = 0; vector<int> nums; for (int i = 1; i <= N; i++) { // If sum of first i natural // numbers exceeds S if (s1 > S) break; s1 += i; // Insert i into nums[] nums.push_back(i); } vector<int> answer; int s2 = 0; // Insert first K - 1 positive // numbers into answer[] for (int i = 0; i < K - 1; i++) { answer.push_back(nums[i]); s2 += nums[i]; } // Insert the K-th number answer.push_back(S - s2); int Max = N; // Traverse the array answer[] for (int i = answer.size() - 1; i >= 0; i--) { // If current element exceeds N if (answer[i] > Max) { int extra = answer[i] - Max; // Add the extra value to // the previous element if (i - 1 >= 0) answer[i - 1] += extra; // Reduce current element to N answer[i] = Max; Max--; } else break; } // Printing the K numbers for (auto x : answer) cout << x << " "; cout << endl;} // Driver Codeint main(){ int S = 15, K = 4, N = 8; solve(S, K, N); return 0;} |
1 2 4 8
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


