aboutsummaryrefslogtreecommitdiff
path: root/challenge-192/deadmarshal/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-192/deadmarshal/cpp/ch-2.cpp')
-rw-r--r--challenge-192/deadmarshal/cpp/ch-2.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-192/deadmarshal/cpp/ch-2.cpp b/challenge-192/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..b6ec0aea97
--- /dev/null
+++ b/challenge-192/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,31 @@
+#include<iostream>
+#include<vector>
+#include<numeric>
+
+int equal_distribution(const std::vector<int>& vec)
+{
+ int sum{},avg{},moves{},sum_part{};
+ sum = std::accumulate(vec.begin(),vec.end(),0);
+ if(sum % vec.size() == 0)
+ {
+ avg = (int)sum / vec.size();
+ for(size_t i = 0; i < vec.size(); ++i)
+ {
+ sum_part += vec[i];
+ moves += abs(sum_part - (avg * (i+1)));
+ }
+ return moves;
+ }
+ return -1;
+}
+
+int main(void)
+{
+ std::vector<int> vec1{1,0,5};
+ std::vector<int> vec2{0,2,0};
+ std::vector<int> vec3{0,3,0};
+ std::cout << equal_distribution(vec1) << '\n';
+ std::cout << equal_distribution(vec2) << '\n';
+ std::cout << equal_distribution(vec3) << '\n';
+ return 0;
+}