aboutsummaryrefslogtreecommitdiff
path: root/challenge-201/deadmarshal/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-201/deadmarshal/cpp/ch-2.cpp')
-rw-r--r--challenge-201/deadmarshal/cpp/ch-2.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-201/deadmarshal/cpp/ch-2.cpp b/challenge-201/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..d05bc157e5
--- /dev/null
+++ b/challenge-201/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,31 @@
+#include<iostream>
+
+#define N 5
+static int count{};
+
+void find_combinations(int *arr,
+ int index,
+ int num,
+ int reduced_num)
+{
+ if(reduced_num < 0) return;
+ if(reduced_num == 0)
+ {
+ count++;
+ return;
+ }
+ int prev = index == 0 ? 1 : arr[index-1];
+ for(int i = prev; i <= num; ++i)
+ {
+ arr[index] = i;
+ find_combinations(arr,index+1,num,reduced_num-i);
+ }
+}
+
+int main()
+{
+ int arr[N];
+ find_combinations(arr,0,N,N);
+ std::cout << count << '\n';
+}
+