aboutsummaryrefslogtreecommitdiff
path: root/challenge-197/paulo-custodio/cpp/ch-1.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-197/paulo-custodio/cpp/ch-1.cpp')
-rw-r--r--challenge-197/paulo-custodio/cpp/ch-1.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-197/paulo-custodio/cpp/ch-1.cpp b/challenge-197/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..a6001446f9
--- /dev/null
+++ b/challenge-197/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,53 @@
+/*
+Challenge 197
+
+Task 1: Move Zero
+Submitted by: Mohammad S Anwar
+You are given a list of integers, @list.
+
+Write a script to move all zero, if exists, to the end while maintaining
+the relative order of non-zero elements.
+
+
+Example 1
+Input: @list = (1, 0, 3, 0, 0, 5)
+Output: (1, 3, 5, 0, 0, 0)
+Example 2
+Input: @list = (1, 6, 4)
+Output: (1, 6, 4)
+Example 3
+Input: @list = (0, 1, 0, 2, 0)
+Output: (1, 2, 0, 0, 0)
+*/
+
+#include <iostream>
+#include <vector>
+
+void move_zeros(std::vector<int>& nums) {
+ std::vector<int> copy=nums;
+ size_t p=0;
+ for (size_t i=0; i<copy.size(); i++)
+ if (copy[i]!=0)
+ nums[p++]=copy[i];
+ for (size_t i=0; i<copy.size(); i++)
+ if (copy[i]==0)
+ nums[p++]=copy[i];
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ if (argc == 0) {
+ std::cerr << "usage: ch-1 nums..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> nums;
+ for (int i=0; i<argc; i++)
+ nums.push_back(atoi(argv[i]));
+
+ move_zeros(nums);
+
+ for (int i=0; i<argc; i++)
+ std::cout << nums[i] << " ";
+ std::cout << std::endl;
+}