aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/paulo-custodio/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-241/paulo-custodio/cpp')
-rw-r--r--challenge-241/paulo-custodio/cpp/ch-1.cpp92
-rw-r--r--challenge-241/paulo-custodio/cpp/ch-2.cpp120
2 files changed, 212 insertions, 0 deletions
diff --git a/challenge-241/paulo-custodio/cpp/ch-1.cpp b/challenge-241/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..13868c150f
--- /dev/null
+++ b/challenge-241/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,92 @@
+/*
+Challenge 241
+
+Task 1: Arithmetic Triplets
+Submitted by: Mohammad S Anwar
+
+You are given an array (3 or more members) of integers in increasing order
+and a positive integer.
+
+Write a script to find out the number of unique Arithmetic Triplets satisfying
+the following rules:
+
+a) i < j < k
+b) nums[j] - nums[i] == diff
+c) nums[k] - nums[j] == diff
+
+Example 1
+
+Input: @nums = (0, 1, 4, 6, 7, 10)
+ $diff = 3
+Output: 2
+
+Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
+Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
+
+Example 2
+
+Input: @nums = (4, 5, 6, 7, 8, 9)
+ $diff = 2
+Output: 2
+
+(0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
+(1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
+*/
+
+#include <iostream>
+#include <string>
+#include <vector>
+using namespace std;
+
+int count_triplets(vector<int>& nums, int diff) {
+ int count = 0;
+ for (int i = 0; i < static_cast<int>(nums.size()) - 2; i++) {
+ for (int j = i + 1; j < static_cast<int>(nums.size()) - 1; j++) {
+ for (int k = j + 1; k < static_cast<int>(nums.size()); k++) {
+ if (nums[j] - nums[i] == diff && nums[k] - nums[j] == diff)
+ count++;
+ }
+ }
+ }
+ return count;
+}
+
+void usage(void) {
+ cerr << "Usage: ch-1 -nums n n n ... -diff n" << endl;
+ exit(EXIT_FAILURE);
+}
+
+int main(int argc, char* argv[]) {
+ vector<int> nums;
+ int diff = -1;
+
+ // parse args
+ int i = 1;
+ while (i < argc) {
+ string arg = argv[i];
+ if (arg == "-nums") {
+ i++;
+ while (i < argc && argv[i][0] != '-') {
+ int n = atoi(argv[i]);
+ nums.push_back(n);
+ i++;
+ }
+ }
+ else if (arg == "-diff") {
+ i++;
+ if (i < argc) {
+ diff = atoi(argv[i]);
+ i++;
+ }
+ }
+ else {
+ usage();
+ }
+ }
+ if (nums.empty() || diff < 0)
+ usage();
+
+ // process
+ int count = count_triplets(nums, diff);
+ cout << count << endl;
+}
diff --git a/challenge-241/paulo-custodio/cpp/ch-2.cpp b/challenge-241/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..c6bee319f8
--- /dev/null
+++ b/challenge-241/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,120 @@
+/*
+Challenge 241
+
+Task 2: Prime Order
+Submitted by: Mohammad S Anwar
+
+You are given an array of unique positive integers greater than 2.
+
+Write a script to sort them in ascending order of the count of their prime
+factors, tie-breaking by ascending value.
+Example 1
+
+Input: @int = (11, 8, 27, 4)
+Output: (11, 4, 8, 27))
+
+Prime factors of 11 => 11
+Prime factors of 4 => 2, 2
+Prime factors of 8 => 2, 2, 2
+Prime factors of 27 => 3, 3, 3
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+using namespace std;
+
+struct Num {
+ int n;
+ vector<int> factors;
+
+ Num(int n);
+ bool operator<(const Num& other);
+};
+
+bool is_prime(int n) {
+ if (n <= 1)
+ return false;
+ if (n <= 3)
+ return true;
+ if ((n % 2) == 0 || (n % 3) == 0)
+ return false;
+ for (int i = 5; i * i <= n; i += 6)
+ if ((n % i) == 0 || (n % (i + 2)) == 0)
+ return false;
+ return true;
+}
+
+int next_prime(int n) {
+ if (n <= 1)
+ return 2;
+ do {
+ n++;
+ } while (!is_prime(n));
+ return n;
+}
+
+vector<int> prime_factors(int n) {
+ vector<int> out;
+ if (n < 2) {
+ int p = 1;
+ out.push_back(p);
+ }
+ else {
+ int p = 2;
+ while (n > 1) {
+ while (n % p == 0) {
+ out.push_back(p);
+ n /= p;
+ }
+ p = next_prime(p);
+ }
+ }
+ return out;
+}
+
+Num::Num(int n) {
+ this->n = n;
+ this->factors = prime_factors(n);
+}
+
+int num_compare(const Num& a, const Num& b) {
+ int a_size = static_cast<int>(a.factors.size());
+ int b_size = static_cast<int>(b.factors.size());
+ if (a_size != b_size)
+ return a_size - b_size;
+ for (size_t i = 0; i < a_size; i++) {
+ if (a.factors[i] != b.factors[i])
+ return a.factors[i] - b.factors[i];
+ }
+ return 0;
+}
+
+bool Num::operator<(const Num& other) {
+ return num_compare(*this, other) < 0 ? true : false;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ cerr << "Usage: ch-2 n n n ..." << endl;
+ exit(EXIT_FAILURE);
+ }
+
+ vector<Num> nums;
+
+ // parse args and compute primes
+ for (int i = 1; i < argc; i++) {
+ int n = atoi(argv[i]);
+ nums.emplace_back(n);
+ }
+
+ // sort
+ sort(nums.begin(), nums.end());
+
+ // output
+ const char* sep = "";
+ for (auto& num : nums) {
+ cout << sep << num.n;
+ sep = " ";
+ }
+}