-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactor_Combinations.cpp
79 lines (62 loc) · 1.54 KB
/
Factor_Combinations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Ms ALEENA is working on numbers; she is fascinated by number factors.
// She is given a number N, she wants to find out all possible ways of
// obtaining N as multiple of its factors.
// For example given N =12
// can be written as multiple of 2,3 and 4.
// and the ways to obtains 12 from ots factors are: 2*2*3, 2*6, 3*4
// Your task is to help Ms ALEENA to find the all possible ways of obtaining N
// from its factors and return all the possible ways as shown in the samples.
// Input Format:
// -------------
// An integer N, value of N.
// Output Format:
// --------------
// Print the list of possiblilties.
// Sample Input-1:
// ---------------
// 12
// Sample Output-1:
// ----------------
// [[2, 2, 3], [2, 6], [3, 4]]
// Sample Input-2:
// ---------------
// 16
// Sample Output-2:
// ----------------
// [[2, 2, 2, 2], [2, 2, 4], [2, 8], [4, 4]]
// Sample Input-3:
// ---------------
// 32
// Sample Output-3:
// ----------------
// [[2, 2, 2, 2, 2], [2, 2, 2, 4], [2, 2, 8], [2, 4, 4], [2, 16], [4, 8]]
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> res;
void solve(vector<int> &v,int n,int ind){
if(n==1){
if(v.size()>1){
res.push_back(v);
}
return;
}
for(int i=ind;i<=n;i++){
if(n%i==0){
v.push_back(i);
solve(v,n/i,i);
v.pop_back();
}
}
}
int main(){
int n;
cin>>n;
vector<int> v;
solve(v,n,2);
for(auto p:res){
for(int i:p){
cout<<i<<" ";
}
cout<<endl;
}
}