Possible numbers with each digit value range given - combinations

We are given 3 variables, d, a and b.
d specifies the maximum number of places.
a and b specify the minimum digit and maximum digit respectively.
We have to find how many numbers can be made each of which has its digits in non-decreasing order, with maximum d places, and each digit between a and b, both inclusive.
Ex-
d = 1
a = 8
b = 9
Answer = 2 (Possible numbers are 8 and 9)
Ex-
d = 2
a = 8
b = 9
Answer = 5 (Possible numbers are 8, 9, 88, 89, and 99)
I have tried applying, permutation and combinations, but could not come across a generic answer for all values of d.
Also, I tried to observe patterns in possible numbers of each digit, but it seems to be varying.
What is the actual approach for it?

You are looking for combinations with repetition. All we have to do is generate all combinations with repetition of your set for each specific length up to d.
Below, we have a simple implementation in C++. First we need the appropriate include along with a function for return the number of combinations with repetition (i.e. nChooseK):
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
int nChooseK(int n, int k) {
int nCk = 1;
for (int i = (n - k + 1), d = 1; d <= k; ++i, ++d) {
nCk *= i;
nCk /= d;
}
return round(nCk);
}
And here is the algorithm for generating combinations with repetition:
void combsWithRep(std::vector<int> v, int d) {
int numIter, maxZ, count = 0;
int n = v.size();
int d1 = d - 1, d2 = d - 2;
std::vector<int> z(d, 0);
int numRows = nChooseK(n + d - 1, d);
maxZ = n - 1;
while (count < numRows) {
numIter = n - z[d1];;
for (int i = 0; i < numIter; ++i, ++count, ++z[d1]) {
for (int k = 0; k < d; ++k) {
std::cout << v[z[k]];
}
std::cout << std::endl;
}
for (int i = d2; i >= 0; i--) {
if (z[i] != maxZ) {
++z[i];
for (int k = (i + 1); k < d; ++k)
z[k] = z[k - 1];
break;
}
}
}
}
And here is the main function:
int main() {
std::vector<int> v;
int d, input;
std::cout << "Enter the number of possible digits" << std::endl;
std::cin >> d;
std::cout << "\nEnter the digits" << std::endl;
while (std::cin >> input)
v.push_back(input);
std::sort(v.begin(), v.end());
for (int i = 1; i <= d; ++i)
combsWithRep(v, i);
return 0;
}
So, for an input of:
Enter the number of possible digits
4
Enter the digits
7
8
9
The output is:
7
8
9
77
78
79
88
89
99
777
778
779
788
789
799
888
889
899
999
7777
7778
7779
7788
7789
7799
7888
7889
7899
7999
8888
8889
8899
8999
9999

Related

Code Chef DSA Learning Series : Multiple of 3

Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that we can't give it to you on the input explicitly; instead, you are only given its starting digits and a way to construct the remainder of the number.
Specifically, you are given d0 and d1; for each i ≥ 2, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold:
Determine if N is a multiple of 3.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers K, d0 and d1.
Output
For each test case, print a single line containing the string "YES" (without quotes) if the number N is a multiple of 3 or "NO" (without quotes) otherwise.
Constraints
1 ≤ T ≤ 1000
2 ≤ K ≤ 1012
1 ≤ d0 ≤ 9
0 ≤ d1 ≤ 9
Example
Input:
3
5 3 4
13 8 1
760399384224 5 1
Output:
NO
YES
YES
Explanation
Example case 1: The whole number N is 34748, which is not divisible by 3, so the answer is NO.
Example case 2: The whole number N is 8198624862486, which is divisible by 3, so the answer is YES.
Question Ended
In this question, in the example test case given, we have k=760399384224, d0=5, and d1=1. Now we know that a number is multiple of 3 if the sum of it’s digits is a multiple of 3. So applying it here, we separate the number n into 3 parts,
Part 1: First 3 digits -> 516 , (5+1+6) mod 3 ==0, so rem1 = 0.
Part 2: Next will be (k-3)/4 times repetition of (2486) ,or,rem2 = ((2+4+8+6)*((k-3)/4))%3= 1
Part 3: the last (k-3)%4 =1 digits which will be 2 (from 2486 repetition) , so rem3 = 2%3=2
So the final answer should be (rem1+rem2+rem3)%3
and I wrote the following code for this logic:
#include<iostream>
#define ll long long
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll k;
cin>>k;
int d0,d1;
cin>>d0>>d1;
int d2 = (d0+d1)%10;
int d[4];
d[0] = (d0+d1+d2)%10;
d[1] = (2*d[0])%10;
d[2] = (2*d[1])%10;
d[3] = (2*d[2])%10;
ll rem1 = (d0+d1+d2)%3,rem2,rem3=0;
rem2 = (20*(((k-3)/4)%3))%3;
ll x = (k-3)%4;
if(x!=0)
{
for(int i=0; i<x; ++i)
rem3+=d[i];
rem3 = rem3%3;
}
else
rem3 =0;
if(k==2)
{
rem1 = (d0+d1)%3;
rem2=0;
rem3=0;
}
if((rem1+rem2+rem3)%3==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
Now they’re giving me WA in their test cases. And I cant think of a possible test cases which doesn’t work with this code. So somebody help me out please.
Here's an apparent mismatch. You can probably use this JavaScript code to find more:
function f(k, d0, d1){
let d2 = (d0+d1)%10;
let d = new Array(4).fill(0);
d[0] = (d0+d1+d2)%10;
d[1] = (2*d[0])%10;
d[2] = (2*d[1])%10;
d[3] = (2*d[2])%10;
let rem1 = (d0+d1+d2)%3, rem2, rem3=0;
rem2 = (20*((~~((k-3)/4))%3))%3;
let x = (k-3)%4;
if(x!=0)
{
for(let i=0; i<x; ++i)
rem3+=d[i];
rem3 = rem3%3;
}
else
rem3 =0;
if(k==2)
{
rem1 = (d0+d1)%3;
rem2=0;
rem3=0;
}
if((rem1+rem2+rem3)%3==0)
return true
return false
}
function bruteForce(k, d0, d1){
let d = (d0 + d1) % 10;
let n = 10 * d0 + d1;
for (let i=3; i<=k; i++){
n = 10 * n + d;
d = (2 * d) % 10;
}
return [n % 3 == 0, n];
}
let str = "";
str += "Examples:\n";
str += "(5, 3, 4)\n" + bruteForce(5, 3, 4) + "\n\n";
str += "(13, 8, 1)\n" + bruteForce(13, 8, 1) + "\n\n"
var k = 7;
var mismatch = false;
for (let i=1; i<10; i++){
for (let j=0; j<10; j++){
const _f = f(k, i, j);
const _bruteForce = bruteForce(k, i, j);
if (_bruteForce[0] != _f){
str += "Mismatch:\n" +
`(${ k }, ${ i }, ${ j })\n` +
"f: " + _f +
"\nbruteForce: " + _bruteForce + "\n\n";
mismatch = true;
}
if (mismatch)
break;
}
if (mismatch)
break;
}
console.log(str);
#include <stdio.h>
void isMulti3(long long int K, long long int d0, long long int d1) {
long long int mod1 = (d0 + d1) % 10;
long long int sum_mod1 = d0 + d1 + mod1;
long long int rep = (K-3)/4;
long long int mod2[4];
mod2[0] = (2*mod1) % 10;
mod2[1] = (4*mod1) % 10;
mod2[2] = (8*mod1) % 10;
mod2[3] = (6*mod1) % 10;
long long int sum_mod2 = 0;
for(int i = 0; i < 4; i++) {
sum_mod2 += mod2[i];
}
long long int unrep = (K - 3) % 4;
long long int sum_mod3 = 0;
for(int i = 0; i < unrep; i++) sum_mod3 += mod2[i];
long long int isMod1 = sum_mod1 % 3;
long long int isMod2 = ((rep%3)*(sum_mod2%3)) % 3;
long long int isMod3 = sum_mod3 % 3;
if((isMod1 + (isMod2 + isMod3)%3)% 3 == 0) printf("YES\n");
else printf("NO\n");
}
int main() {
int T;
scanf("%d", &T);
for(int i = 0; i < T; i++) {
long long int K;
long long int d0, d1;
scanf("%lld %lld %lld", &K, &d0, &d1);
if(K == 2) {
if((d0 + d1) % 3 == 0) printf("YES\n");
else printf("NO\n");
}
else isMulti3(K, d0, d1);
}
}
I think you are the same guy who asked the question on the codechef discussion portal.
I answered it there , and I am sharing the link.
[https://discuss.codechef.com/t/dsa-learning-series-multiple-of-3/77174/4?u=nazishkaunain][1]
So the point where you are mistaken is:
You might use the fact:
(a+b+c)mod 3 != a mod 3 + b mod 3 + c mod 3;
But (a + b + c) mod 3 = (a mod 3 + ( b mod 3 + c mod 3) mod 3) mod 3;
And a number n ( n = a + b + c) is divisible by 3 only if n mod 3 = 0 => (a + b + c) mod 3 = 0.

Every time I change the constant, the result is different

I want to code the 01 Knapsack problem with c++. Each time I change the value of a constant MAX to a different value, such as 100,90, or 80, the result is different. What's the cause? I'm using Visual Studio 2019.
The input is a text file, for example.
20
40 35 18 4 10 2 70 20 39 37 7 5 10 8 15 21 50 40 10 30
100 50 45 20 10 5 31 10 20 19 4 3 6 8 12 7 10 2 5 5
137
The first row is the number of objects, the second row is the price, the third row is the weight, and the last row is the size of the knapsack.
#include <stdio.h>
#include <time.h>
#define MAX 100
#define CLOCKS_PER_MS CLOCKS_PER_SEC/1000
int X_d[MAX] = { 0 }; //solution vector
int max(int a, int b) {
if (a >= b)
return a;
else
return b;
}
void dynamic(int n, int M, int p[], int w[]) {
int result;
int i, y, k;
int P[MAX][MAX] = { 0 };
clock_t start, finish;
start = clock();
for (i = 0; i <= n; i++) {
for (y = 0; y <= M; y++) {
if (i == 0 || y == 0)
P[i][y] = 0;
else if (w[i - 1] > y)
P[i][y] = P[i - 1][y];
else
P[i][y] = max(P[i - 1][y], p[i - 1] + P[i - 1][y - w[i - 1]]);
}
}
finish = clock();
result = P[n][M];
y = M;
for (i = n; i > 0 && result > 0; i--) {
if (result == P[i - 1][y]) {
continue;
}
else {
X_d[i - 1] = 1;
result = result - p[i - 1];
y = y - w[i - 1];
}
}
printf("\n(1) Dynamic Programming");
printf("\nThe maximum profit is $%d", P[n][M]);
printf("\nThe solution vetor X = ( ");
for (k = 0; k < n; k++)
printf("%d ", X_d[k]);
printf(")\n");
printf("The execution time is %f milliseconds.\n", (float)(finish - start) / CLOCKS_PER_MS);
}
int main() {
int i, j;
int num, M;
int p[MAX] = { 0 }, w[MAX] = { 0 };
FILE* fp = NULL;
fopen_s(&fp, "p2data6.txt", "r");
fscanf_s(fp, "%d", &num);
for (i = 0; i < num; i++)
fscanf_s(fp, "%d", &p[i]);
for (i = 0; i < num; i++)
fscanf_s(fp, "%d", &w[i]);
fscanf_s(fp, "%d", &M);
printf("n = %d\n", num);
printf("pi = ");
for (i = 0; i < num; i++)
printf("%3d ", p[i]);
printf("\nwi = ");
for (i = 0; i < num; i++)
printf("%3d ", w[i]);
printf("\npi/wi = ");
for (i = 0; i < num; i++)
printf("%f ", (double)p[i] / w[i]);
printf("\nM = %d\n", M);
dynamic(num, M, p, w);
return 0;
}
Geeks for Geeks has a very good explanation of this problem here:
To quote the Geeks for Geeks article by Sam007:
A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.
1) Optimal Substructure:
To consider all subsets of items, there can be two cases for every item: (1) the item is included in the optimal subset, (2) not included in the optimal set.
Therefore, the maximum value that can be obtained from n items is max of following two values.
1) Maximum value obtained by n-1 items and W weight (excluding nth item).
2) Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item).
If weight of nth item is greater than W, then the nth item cannot be included and case 1> is the only possibility.
2) Overlapping Subproblems
Following is recursive implementation that simply follows the recursive structure mentioned above.

For loop divide numbers

I'm an amateur when it comes to C++ but I've already received a task which is over my knowledge.
Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.
EDIT:(18.10.15)
Turns out I didn't understood my task correctly. Here it is:
"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."
For example, I enter 100 and 200. In this interval there's 153.
153 = 1^3 + 5^3 + 3^3 (1+125+27)
Programme shows 153.
cin >> n;
cin >> m;
for (int i=n; i<=m; i++)
{
for (int k=n; k<=i; k++)
{
a = n % 10; //for example, I enter 153, then a=3
f = n /= 10; //f=15
b = f % 10; //b=5
f = f /= 10; //f=1
c = f % 10; //c=1
f = f /= 10;
d = f % 10;
for (int j=1; j<=5; j++)
{
a = a * a;
b = b * b;
c = c * c;
d = d * d;
if (a + b + c + d == n)
{
cout << n << endl;
}
}
}
}
Any help will be appreciated.
Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.
Assuming the range is given as [n, m), then here's your program:
return (n != m);
Any number can be seen as a sum of numbers with the same exponent. For example:
0 = 0 ^ 3 + 0 ^ 3 + 0 ^ 3
1 = 1 ^ 3 + 0 ^ 3
2 = 1 ^ 3 + 1 ^ 3
3 = 1 ^ 3 + 1 ^ 3 + 1 ^ 3
and so on. This is true even for negative numbers.
So in any non-empty range there exists at least 1 such number.
"All I know is how to get the programm to check each number separately"
"Programme must not use arrays."
for (int i = n; i <= m; i++) {
...
int x = (int)Math.log10(i);
int rest = i;
for (int p = x; p>=0; p--) {
int digit = rest / (int)Math.pow(10,p);
rest = i % (int)Math.pow(10,p);
//3802 = 3*10^3 + 8*10^2 + 0*10^1 + 2*10^0
}
}
...
Sorry, is Java no C++
Sorry that I answer so late and that I phrased my question poorly - English isn't my native language.
But turns out I didn't understood my task correctly. Here it is:
"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."
For example, I enter 100 and 200. In this interval there's 153.
153 = 1^3 + 5^3 + 3^3 (1+125+27)
Programme shows 153.
cin >> n;
cin >> m;
for (int i=n; i<=m; i++)
{
for (int k=n; k<=i; k++)
{
a = n % 10; //for example, I enter 153, then a=3
f = n /= 10; //f=15
b = f % 10; //b=5
f = f /= 10; //f=1
c = f % 10; //c=1
f = f /= 10;
d = f % 10;
for (int j=1; j<=5; j++)
{
a = a * a;
b = b * b;
c = c * c;
d = d * d;
if (a + b + c + d == n)
{
cout << n << endl;
}
}
}
}

Find the sum of digits of a sequence of integers

I made up my mind to write a little piece of code that gets two integers, lets say M and N ( M <= N ) and sum the digits of all the integers between them, inclusive. So for example if M = 1 and N = 9, DigitSum will equal to 45. If M = 10 and N = 11 the sum will be (1 + 0 (10) + 1 + 1 (11) = 3).
Here is my code so far (Done the for loop instead of the return):
#include <iostream>
#include <vector>
using namespace std;
// the partial digits sums digitSum[i] = the sum of the digits between 0 and i
int digitSum[] = {0, 1, 3, 6, 10, 15, 21, 28, 36, 45};
int pow_of_ten[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
// the sums of all the digits in the numbers from 1 to (10^(i) - 1) where i is the index in the array
long subsums[] = {0, 45, 20 * 45, 300 * 45, 4000 * 45, 50000 * 45, 600000 * 45, 7000000 * 45, 80000000 * 45,
900000000 * 45};
//Calculates the sum of all digits between 0 and M inclusive
long Digit_Sum(int M) {
if (M < 10) {
return digitSum[M];
}
long result = 0;
int same = M;
int counter = 0;
int lastdigit = 0;
while (same > 0) {
if (same < 10) {
lastdigit = same;
break;
}
same /= 10;
counter ++;
}
for(;counter >= 0; counter --) {
result += (subsums[counter] + M % pow_of_ten[counter] + 1) * lastdigit;
result += digitSum[lastdigit - 1] * pow_of_ten[counter];
if (counter == 0) {
break;
}
lastdigit = (M / pow_of_ten[counter - 1]) % 10;
}
return result;
}
int main() {
int M;
int N;
vector<long> sums;
while (true) {
cin >> M >> N;
if (M == 0 && N == 0) {
break;
}
sums.push_back(Digit_Sum(N) - Digit_Sum(M - 1));
}
for (vector<long>::iterator it = sums.begin(); it != sums.end(); ++it) {
cout << *it << endl;
}
}
For most cases this works well but an Online judge says it is wrong. I looked at other solutions that work but no one hard-coded the values in arrays the way I did. May this cause a partial problem, any ideas?
You can easily just create a for-loop to greatly simplify this code.
There is no need to go through all that effort.
for (Initialization Action, Boolean Expression, Update_Action)
Re deletion below: sorry, I have a bit influenza and mizread N as M. :(
I think a main error is M-1 in
sums.push_back(Digit_Sum(N) - Digit_Sum(M - 1));
Also noting that <when corrected that formula will only work for single-digit numbers. My comment earlier about using a simple formula was based on misunderstanding your problem description, in view of that formula and your examples. Both indicated single digit numbers only.
However, the complexity of the code appears unreasonably high. Consider this, assuming non-negative integers as input, and assuming m is always less than or equal to n:
#include <iostream>
#include <stdexcept>
using namespace std;
bool throwX() { throw std::runtime_error( "Ouch." ); }
auto main() -> int
{
for( ;; )
{
int m, n;
cin >> m >> n || throwX();
if( m == 0 && n == 0 ) { break; }
int sum = 0;
for( int i = m; i <= n; ++i )
{
for( int v = i; v != 0; v /= 10 )
{
sum += v % 10;
}
}
cout << sum << endl;
}
}
It needs not be more complicated than that.
Tested and working to spec, sans console input:
#include <iostream>
#include <string>
using namespace std;
void sum_a_to_b(const int & a, const int & b)
{
if (a <= b && a >= 0)
{
long long sum = 0;
for (int i = a; i <= b; i++)
{
sum += i;
}
cout << "Sum of digits from " << a << " through " << b << " is " << sum << ".\n";
}
}
int main()
{
sum_a_to_b(5, 6);
sum_a_to_b(1, 9);
}

C++ Newbie needs helps for printing combinations of integers

Suppose I am given:
A range of integers iRange (i.e. from 1 up to iRange) and
A desired number of combinations
I want to find the number of all possible combinations and print out all these combinations.
For example:
Given: iRange = 5 and n = 3
Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 5! / (5-3)! * 3! = 10 combinations, and the output is:
123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345
Another example:
Given: iRange = 4 and n = 2
Then the number of combinations is iRange! / ((iRange!-n!)*n!) = 4! / (4-2)! * 2! = 6 combinations, and the output is:
12 - 13 - 14 - 23 - 24 - 34
My attempt so far is:
#include <iostream>
using namespace std;
int iRange= 0;
int iN=0;
int fact(int n)
{
if ( n<1)
return 1;
else
return fact(n-1)*n;
}
void print_combinations(int n, int iMxM)
{
int iBigSetFact=fact(iMxM);
int iDiffFact=fact(iMxM-n);
int iSmallSetFact=fact(n);
int iNoTotComb = (iBigSetFact/(iDiffFact*iSmallSetFact));
cout<<"The number of possible combinations is: "<<iNoTotComb<<endl;
cout<<" and these combinations are the following: "<<endl;
int i, j, k;
for (i = 0; i < iMxM - 1; i++)
{
for (j = i + 1; j < iMxM ; j++)
{
//for (k = j + 1; k < iMxM; k++)
cout<<i+1<<j+1<<endl;
}
}
}
int main()
{
cout<<"Please give the range (max) within which the combinations are to be found: "<<endl;
cin>>iRange;
cout<<"Please give the desired number of combinations: "<<endl;
cin>>iN;
print_combinations(iN,iRange);
return 0;
}
My problem:
The part of my code related to the printing of the combinations works only for n = 2, iRange = 4 and I can't make it work in general, i.e., for any n and iRange.
Your solution will only ever work for n=2. Think about using an array (combs) with n ints, then the loop will tick up the last item in the array. When that item reaches max update then comb[n-2] item and set the last item to the previous value +1.
Basically working like a clock but you need logic to find what to uptick and what the next minimum value is.
Looks like a good problem for recursion.
Define a function f(prefix, iMin, iMax, n), that prints all combinations of n digits in the range [iMin, iMax] and returns the total number of combinations. For n = 1, it should print every digit from iMin to iMax and return iMax - iMin + 1.
For your iRange = 5 and n = 3 case, you call f("", 1, 5, 3). The output should be 123 - 124 - 125 - 134 - 135 - 145 - 234 - 235 - 245 - 345.
Notice that the first group of outputs are simply 1 prefixed onto the outputs of f("", 2, 5, 2), i.e. f("1", 2, 5, 2), followed by f("2", 3, 5, 2) and f("3", 4, 5, 2). See how you would do that with a loop. Between this, the case for n = 1 above, and traps for bad inputs (best if they print nothing and return 0, it should simplify your loop), you should be able to write f().
I'm stopping short because this looks like a homework assignment. Is this enough to get you started?
EDIT: Just for giggles, I wrote a Python version. Python has an easier time throwing around sets and lists of things and staying legible.
#!/usr/bin/env python
def Combos(items, n):
if n <= 0 or len(items) == 0:
return []
if n == 1:
return [[x] for x in items]
result = []
for k in range(len(items) - n + 1):
for s in Combos(items[k+1:], n - 1):
result.append([items[k]] + s)
return result
comb = Combos([str(x) for x in range(1, 6)], 3)
print len(comb), " - ".join(["".join(c) for c in comb])
Note that Combos() doesn't care about the types of the items in the items list.
Here is your code edited :D :D with a recursive solution:
#include <iostream>
int iRange=0;
int iN=0; //Number of items taken from iRange, for which u want to print out the combinations
int iTotalCombs=0;
int* pTheRange;
int* pTempRange;
int find_factorial(int n)
{
if ( n<1)
return 1;
else
return find_factorial(n-1)*n;
}
//--->Here is another solution:
void print_out_combinations(int *P, int K, int n_i)
{
if (K == 0)
{
for (int j =iN;j>0;j--)
std::cout<<P[j]<<" ";
std::cout<<std::endl;
}
else
for (int i = n_i; i < iRange; i++)
{
P[K] = pTheRange[i];
print_out_combinations(P, K-1, i+1);
}
}
//Here ends the solution...
int main()
{
std::cout<<"Give the set of items -iRange- = ";
std::cin>>iRange;
std::cout<<"Give the items # -iN- of iRange for which the combinations will be created = ";
std::cin>>iN;
pTheRange = new int[iRange];
for (int i = 0;i<iRange;i++)
{
pTheRange[i]=i+1;
}
pTempRange = new int[iN];
iTotalCombs = (find_factorial(iRange)/(find_factorial(iRange-iN)*find_factorial(iN)));
std::cout<<"The number of possible combinations is: "<<iTotalCombs<<std::endl;
std::cout<<"i.e.the combinations of "<<iN<<" elements drawn from a set of size "<<iRange<<" are: "<<std::endl;
print_out_combinations(pTempRange, iN, 0);
return 0;
}
Here's an example of a plain recursive solution. I believe there exists a more optimal implementation if you replace recursion with cycles. It could be your homework :)
#include <stdio.h>
const int iRange = 9;
const int n = 4;
// A more efficient way to calculate binomial coefficient, in my opinion
int Cnm(int n, int m)
{
int i;
int result = 1;
for (i = m + 1; i <= n; ++i)
result *= i;
for (i = n - m; i > 1; --i)
result /= i;
return result;
}
print_digits(int *digits)
{
int i;
for (i = 0; i < n; ++i) {
printf("%d", digits[i]);
}
printf("\n");
}
void plus_one(int *digits, int index)
{
int i;
// Increment current digit
++digits[index];
// If it is the leftmost digit, run to the right, setup all the others
if (index == 0) {
for (i = 1; i < n; ++i)
digits[i] = digits[i-1] + 1;
}
// step back by one digit recursively
else if (digits[index] > iRange) {
plus_one(digits, index - 1);
}
// otherwise run to the right, setting up other digits, and break the recursion once a digit exceeds iRange
else {
for (i = index + 1; i < n; ++i) {
digits[i] = digits[i-1] + 1;
if (digits[i] > iRange) {
plus_one(digits, i - 1);
break;
}
}
}
}
int main()
{
int i;
int digits[n];
for (i = 0; i < n; ++i) {
digits[i] = i + 1;
}
printf("%d\n\n", Cnm(iRange, n));
// *** This loop has been updated ***
while (digits[0] <= iRange - n + 1) {
print_digits(digits);
plus_one(digits, n - 1);
}
return 0;
}
This is my C++ function with different interface (based on sts::set) but performing the same task:
typedef std::set<int> NumbersSet;
typedef std::set<NumbersSet> CombinationsSet;
CombinationsSet MakeCombinations(const NumbersSet& numbers, int count)
{
CombinationsSet result;
if (!count) throw std::exception();
if (count == numbers.size())
{
result.insert(NumbersSet(numbers.begin(), numbers.end()));
return result;
}
// combinations with 1 element
if (!(count - 1) || (numbers.size() <= 1))
{
for (auto number = numbers.begin(); number != numbers.end(); ++number)
{
NumbersSet single_combination;
single_combination.insert(*number);
result.insert(single_combination);
}
return result;
}
// Combinations with (count - 1) without current number
int first_num = *numbers.begin();
NumbersSet truncated_numbers = numbers;
truncated_numbers.erase(first_num);
CombinationsSet subcombinations = MakeCombinations(truncated_numbers, count - 1);
for (auto subcombination = subcombinations.begin(); subcombination != subcombinations.end(); ++subcombination)
{
NumbersSet cmb = *subcombination;
// Add current number
cmb.insert(first_num);
result.insert(cmb);
}
// Combinations with (count) without current number
subcombinations = MakeCombinations(truncated_numbers, count);
result.insert(subcombinations.begin(), subcombinations.end());
return result;
}
I created a next_combination() function similar to next_permutation(), but valid input is required to make it work
//nums should always be in ascending order
vector <int> next_combination(vector<int>nums, int max){
int size = nums.size();
if(nums[size-1]+1<=max){
nums[size-1]++;
return nums;
}else{
if(nums[0] == max - (size -1)){
nums[0] = -1;
return nums;
}
int pos;
int negate = -1;
for(int i = size-2; i>=0; i--){
if(nums[i]+1 <= max + negate){
pos = i;
break;
}
negate --;
}
nums[pos]++;
pos++;
while(pos<size){
nums[pos] = nums[pos-1]+1;
pos++;
}
}
return nums;
}