
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to choose some numbers for which there will be no subset whose sum is k
Suppose we have two numbers n and k. We have to choose the maximum number of distinct elements from 1 to n, so that no subset whose sum is equal to k. If we can find then return the chosen numbers.
So, if the input is like n = 5; k = 3, then the output will be [4, 5, 2]
Steps
To solve this, we will follow these steps −
for initialize i := (k + 1) / 2, when i <= k - 1, update (increase i by 1), do: print i for initialize i := k + 1, when i <= n, update (increase i by 1), do: print i
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n, int k) { for (int i = (k + 1) / 2; i <= k - 1; i++) { cout << i << ", "; } for (int i = k + 1; i <= n; i++) { cout << i << ", "; } } int main() { int n = 5; int k = 3; solve(n, k); }
Input
5, 3
Output
2, 4, 5,
- Related Articles
- Python Program for Subset Sum Problem
- C++ program to count minimum how many minutes after there will be no new angry students
- Find the values of \( k \) for which the system\(2 x+k y=1\)\( 3 x-5 y=7 \)will have no solution. Is there a value of $k$ for which the system has infinitely many solutions?
- Program to count number of paths whose sum is k in python
- What will happen if there will be no cone cells in our retina?
- C / C++ Program for Subset Sum (Backtracking)
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++
- Program to find sum of rectangle whose sum at most k in Python
- For which value(s) of \( k \) will the pair of equations\( k x+3 y=k-3 \)\( 12 x+k y=k \)have no solution?
- Program to find three unique elements from list whose sum is closest to k Python
- Program to find product of few numbers whose sum is given in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- How are we able to see in dreams as there is no light that should be reflected, which is important for seeing objects?

Advertisements