Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<vector<int>> res;
- void CombinationRepetitionUtil(int chosen[], int arr[], int index, int r, int start, int end) {
- if (index == r) {
- vector<int> vec;
- for (int i = 0; i < r; i++) {
- vec.push_back(arr[chosen[i]]);
- }
- res.push_back(vec);
- return;
- }
- for (int i = start; i <= end; i++) {
- chosen[index] = i;
- CombinationRepetitionUtil(chosen, arr, index + 1, r, i, end);
- }
- return;
- }
- void CombinationRepetition(int arr[], int n, int r) {
- int chosen[r + 1];
- CombinationRepetitionUtil(chosen, arr, 0, r, 0, n - 1);
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int arr[] = {0, 1, 2, 3};
- int n = sizeof(arr) / sizeof(arr[0]);
- int r = 2;
- // generate all combinations of size r (using the elements from the array)
- CombinationRepetition(arr, n, r);
- for (int i = 0; i < (int) res.size(); i++) {
- for (int j = 0; j < (int) res[i].size(); j++) {
- cout << res[i][j] << " ";
- }
- cout << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement