aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-03-01 11:54:57 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2023-03-01 11:54:57 +0000
commit733888b34da6621ad5bcb2781a66da355c0e6f22 (patch)
tree6e8e37a2cfb64e1f974cbe2fded04eb2b653017d
parent8eab5fba3eed3e714ca70a7f8180edf2e6e32546 (diff)
downloadperlweeklychallenge-club-733888b34da6621ad5bcb2781a66da355c0e6f22.tar.gz
perlweeklychallenge-club-733888b34da6621ad5bcb2781a66da355c0e6f22.tar.bz2
perlweeklychallenge-club-733888b34da6621ad5bcb2781a66da355c0e6f22.zip
Add C and C++ solutions
-rw-r--r--challenge-205/paulo-custodio/c/ch-1.c71
-rw-r--r--challenge-205/paulo-custodio/c/ch-2.c61
-rw-r--r--challenge-205/paulo-custodio/cpp/ch-1.cpp65
-rw-r--r--challenge-205/paulo-custodio/cpp/ch-2.cpp59
-rw-r--r--challenge-206/paulo-custodio/c/ch-1.c64
-rw-r--r--challenge-206/paulo-custodio/c/ch-2.c130
-rw-r--r--challenge-206/paulo-custodio/cpp/ch-1.cpp62
-rw-r--r--challenge-206/paulo-custodio/cpp/ch-2.cpp84
-rw-r--r--challenge-206/paulo-custodio/perl/ch-1.pl32
-rw-r--r--challenge-206/paulo-custodio/perl/ch-2.pl66
10 files changed, 475 insertions, 219 deletions
diff --git a/challenge-205/paulo-custodio/c/ch-1.c b/challenge-205/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..d5243c079c
--- /dev/null
+++ b/challenge-205/paulo-custodio/c/ch-1.c
@@ -0,0 +1,71 @@
+/*
+Challenge 205
+
+Task 1: Third Highest
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find out the Third Highest if found otherwise return the maximum.
+Example 1
+
+Input: @array = (5,3,4)
+Output: 3
+
+First highest is 5. Second highest is 4. Third highest is 3.
+
+Example 2
+
+Input: @array = (5,6)
+Output: 6
+
+First highest is 6. Second highest is 5. Third highest is missing, so maximum is returned.
+
+Example 3
+
+Input: @array = (5,4,4,3)
+Output: 3
+
+First highest is 5. Second highest is 4. Third highest is 3.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int compare_desc(const void* a, const void* b) {
+ return *(int*)b - *(int*)a;
+}
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ int result = 0;
+
+ if (argc > 0) {
+ int num_items = argc;
+ int* items = malloc(num_items * sizeof(int));
+
+ // parse input numbers
+ for (int i = 0; i < num_items; i++)
+ items[i] = atoi(argv[i]);
+
+ // sort descending
+ qsort(items, num_items, sizeof(int), compare_desc);
+
+ // remove duplicates
+ int last = items[0]+1; // different from first
+ int j = 0;
+ for (int i = 0; i < num_items; i++) {
+ if (items[i] != last)
+ items[j++] = last = items[i];
+ }
+ num_items = j;
+
+ // output
+ result = (num_items < 3) ? items[0] : items[2];
+
+ free(items);
+ }
+
+ printf("%d\n", result);
+ return EXIT_SUCCESS;
+}
diff --git a/challenge-205/paulo-custodio/c/ch-2.c b/challenge-205/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..78c8f07ab1
--- /dev/null
+++ b/challenge-205/paulo-custodio/c/ch-2.c
@@ -0,0 +1,61 @@
+/*
+Challenge 205
+
+Task 2: Maximum XOR
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find the highest value obtained by XORing any two distinct members of the array.
+Example 1
+
+Input: @array = (1,2,3,4,5,6,7)
+Output: 7
+
+The maximum result of 1 xor 6 = 7.
+
+Example 2
+
+Input: @array = (2,4,1,3)
+Output: 7
+
+The maximum result of 4 xor 3 = 7.
+
+Example 3
+
+Input: @array = (10,5,7,12,8)
+Output: 15
+
+The maximum result of 10 xor 5 = 15.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ int result = -1;
+
+ if (argc > 0) {
+ int num_items = argc;
+ int* items = malloc(num_items * sizeof(int));
+
+ // parse input numbers
+ for (int i = 0; i < num_items; i++)
+ items[i] = atoi(argv[i]);
+
+ // compute max of xor of all pairs
+ for (int i = 0; i < num_items-1; i++) {
+ for (int j = i+1; j < num_items; j++) {
+ int n = items[i] ^ items[j];
+ if (n > result)
+ result = n;
+ }
+ }
+
+ free(items);
+ }
+
+ printf("%d\n", result);
+ return EXIT_SUCCESS;
+}
diff --git a/challenge-205/paulo-custodio/cpp/ch-1.cpp b/challenge-205/paulo-custodio/cpp/ch-1.cpp
new file mode 100644
index 0000000000..54e83bebfb
--- /dev/null
+++ b/challenge-205/paulo-custodio/cpp/ch-1.cpp
@@ -0,0 +1,65 @@
+/*
+Challenge 205
+
+Task 1: Third Highest
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find out the Third Highest if found otherwise return the maximum.
+Example 1
+
+Input: @array = (5,3,4)
+Output: 3
+
+First highest is 5. Second highest is 4. Third highest is 3.
+
+Example 2
+
+Input: @array = (5,6)
+Output: 6
+
+First highest is 6. Second highest is 5. Third highest is missing, so maximum is returned.
+
+Example 3
+
+Input: @array = (5,4,4,3)
+Output: 3
+
+First highest is 5. Second highest is 4. Third highest is 3.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ int result = 0;
+
+ if (argc > 0) {
+ std::vector<int> items;
+
+ // parse input numbers
+ for (int i = 0; i < argc; i++)
+ items.push_back(atoi(argv[i]));
+
+ // sort descending
+ std::sort(items.rbegin(), items.rend());
+
+ // remove duplicates
+ int last = items[0]+1; // different from first
+ size_t j = 0;
+ for (size_t i = 0; i < items.size(); i++) {
+ if (items[i] != last)
+ items[j++] = last = items[i];
+ }
+ items.resize(j);
+
+ // output
+ result = (items.size() < 3) ? items[0] : items[2];
+ }
+
+ std::cout << result << std::endl;
+ return EXIT_SUCCESS;
+}
diff --git a/challenge-205/paulo-custodio/cpp/ch-2.cpp b/challenge-205/paulo-custodio/cpp/ch-2.cpp
new file mode 100644
index 0000000000..6eb130b24a
--- /dev/null
+++ b/challenge-205/paulo-custodio/cpp/ch-2.cpp
@@ -0,0 +1,59 @@
+/*
+Challenge 205
+
+Task 2: Maximum XOR
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to find the highest value obtained by XORing any two distinct members of the array.
+Example 1
+
+Input: @array = (1,2,3,4,5,6,7)
+Output: 7
+
+The maximum result of 1 xor 6 = 7.
+
+Example 2
+
+Input: @array = (2,4,1,3)
+Output: 7
+
+The maximum result of 4 xor 3 = 7.
+
+Example 3
+
+Input: @array = (10,5,7,12,8)
+Output: 15
+
+The maximum result of 10 xor 5 = 15.
+*/
+
+#include <algorithm>
+#include <iostream>
+#include <vector>
+
+int main(int argc, char* argv[]) {
+ argv++; argc--;
+ int result = -1;
+
+ if (argc > 0) {
+ std::vector<int> items;
+
+ // parse input numbers
+ for (int i = 0; i < argc; i++)
+ items.push_back(atoi(argv[i]));
+
+ // compute max of xor of all pairs
+ for (size_t i = 0; i < items.size()-1; i++) {
+ for (size_t j = i+1; j < items.size(); j++) {
+ int n = items[i] ^ items[j];
+ if (n > result)
+ result = n;
+ }
+ }
+ }
+
+ std::cout << result << std::endl;
+ return EXIT_SUCCESS;
+}
diff --git a/challenge-206/paulo-custodio/c/ch-1.c b/challenge-206/paulo-custodio/c/ch-1.c
index 61d77fb946..abb788e056 100644
--- a/challenge-206/paulo-custodio/c/ch-1.c
+++ b/challenge-206/paulo-custodio/c/ch-1.c
@@ -29,42 +29,42 @@ Output: 15
#include <stdlib.h>
int minutes(const char* str) {
- int hours = 0, minutes = 0;
- if (sscanf(str, "%d:%d", &hours, &minutes) == 2)
- return hours*60+minutes;
- else
- return 0;
+ int hours = 0, minutes = 0;
+ if (sscanf(str, "%d:%d", &hours, &minutes) == 2)
+ return hours*60+minutes;
+ else
+ return 0;
}
int compare(const void* a, const void* b) {
- return *(int*)a - *(int*)b;
+ return *(int*)a - *(int*)b;
}
int main(int argc, char* argv[]) {
- argv++; argc--;
- if (argc < 2) {
- fputs("Usage: ch-1 time...\n", stderr);
- return EXIT_FAILURE;
- }
-
- int num_items = argc+1;
- int* items = malloc(num_items * sizeof(int));
- for (int i = 0; i < num_items-1; i++)
- items[i] = minutes(argv[i]);
- qsort(items, num_items-1, sizeof(int), compare);
- items[num_items-1] = items[0] + 24*60;
-
- int min = items[num_items-1] - items[0];
- for (int i = 0; i < num_items-1; i++) {
- for (int j = i+1; j < num_items; j++) {
- int n = items[j] - items[i];
- if (n < min)
- min = n;
- }
- }
-
- printf("%d\n", min);
- free(items);
-
- return EXIT_SUCCESS;
+ argv++; argc--;
+ if (argc < 2) {
+ fputs("Usage: ch-1 time...\n", stderr);
+ return EXIT_FAILURE;
+ }
+
+ int num_items = argc+1;
+ int* items = malloc(num_items * sizeof(int));
+ for (int i = 0; i < num_items-1; i++)
+ items[i] = minutes(argv[i]);
+ qsort(items, num_items-1, sizeof(int), compare);
+ items[num_items-1] = items[0] + 24*60;
+
+ int min = items[num_items-1] - items[0];
+ for (int i = 0; i < num_items-1; i++) {
+ for (int j = i+1; j < num_items; j++) {
+ int n = items[j] - items[i];
+ if (n < min)
+ min = n;
+ }
+ }
+
+ printf("%d\n", min);
+ free(items);
+
+ return EXIT_SUCCESS;
}
diff --git a/challenge-206/paulo-custodio/c/ch-2.c b/challenge-206/paulo-custodio/c/ch-2.c
index baed295d4c..79e72feeb5 100644
--- a/challenge-206/paulo-custodio/c/ch-2.c
+++ b/challenge-206/paulo-custodio/c/ch-2.c
@@ -41,94 +41,94 @@ So the maximum sum is 2.
#define MAX(a,b) ((a) > (b) ? (a) : (b))
typedef struct {
- size_t count;
- int* items;
+ size_t count;
+ int* items;
} IList;
IList* ilist_new(size_t count) {
- IList* list = calloc(1, sizeof(IList));
- assert(list);
- if (count) {
- list->count = count;
- list->items = calloc(count, sizeof(int));
- assert(list->items);
- }
- return list;
+ IList* list = calloc(1, sizeof(IList));
+ assert(list);
+ if (count) {
+ list->count = count;
+ list->items = calloc(count, sizeof(int));
+ assert(list->items);
+ }
+ return list;
}
void ilist_free(IList* list) {
- free(list->items);
- free(list);
+ free(list->items);
+ free(list);
}
void ilist_push(IList* list, int elem) {
- list->count++;
- list->items = realloc(list->items, list->count * sizeof(int));
- assert(list->items);
- list->items[list->count - 1] = elem;
+ list->count++;
+ list->items = realloc(list->items, list->count * sizeof(int));
+ assert(list->items);
+ list->items[list->count - 1] = elem;
}
void ilist_remove(IList* list, size_t index) {
- assert(index < list->count);
- memmove(&list->items[index], &list->items[index+1], (list->count - (index+1))*sizeof(int));
- list->count--;
+ assert(index < list->count);
+ memmove(&list->items[index], &list->items[index+1], (list->count - (index+1))*sizeof(int));
+ list->count--;
}
IList* ilist_clone(IList* list) {
- IList* copy = ilist_new(list->count);
- assert(copy);
- memcpy(copy->items, list->items, list->count * sizeof(int));
- return copy;
+ IList* copy = ilist_new(list->count);
+ assert(copy);
+ memcpy(copy->items, list->items, list->count * sizeof(int));
+ return copy;
}
void compute_pairs_max(int* max, IList* set, IList* pending) {
- if (pending->count == 0) { // compute sum, set max
- int sum = 0;
- for (int i = 0; i < set->count; i += 2) {
- int n = MIN(set->items[i], set->items[i + 1]);
- sum += n;
- }
- *max = MAX(*max, sum);
- }
- else { // recurse for each pair
- for (int i = 0; i < pending->count - 1; i++) {
- for (int j = i+1; j < pending->count; j++) {
- IList* new_set = ilist_clone(set);
- IList* new_pending = ilist_clone(pending);
-
- ilist_push(new_set, pending->items[i]);
- ilist_push(new_set, pending->items[j]);
-
- ilist_remove(new_pending, j);
- ilist_remove(new_pending, i);
-
- compute_pairs_max(max, new_set, new_pending);
-
- ilist_free(new_set);
- ilist_free(new_pending);
- }
- }
- }
+ if (pending->count == 0) { // compute sum, set max
+ int sum = 0;
+ for (int i = 0; i < set->count; i += 2) {
+ int n = MIN(set->items[i], set->items[i + 1]);
+ sum += n;
+ }
+ *max = MAX(*max, sum);
+ }
+ else { // recurse for each pair
+ for (int i = 0; i < pending->count - 1; i++) {
+ for (int j = i+1; j < pending->count; j++) {
+ IList* new_set = ilist_clone(set);
+ IList* new_pending = ilist_clone(pending);
+
+ ilist_push(new_set, pending->items[i]);
+ ilist_push(new_set, pending->items[j]);
+
+ ilist_remove(new_pending, j);
+ ilist_remove(new_pending, i);
+
+ compute_pairs_max(max, new_set, new_pending);
+
+ ilist_free(new_set);
+ ilist_free(new_pending);
+ }
+ }
+ }
}
int main(int argc, char* argv[]) {
- argv++; argc--;
- if (argc % 2 != 0) {
- fputs("usage: ch-2 pairs...\n", stderr);
- return EXIT_FAILURE;
- }
+ argv++; argc--;
+ if (argc % 2 != 0) {
+ fputs("usage: ch-2 pairs...\n", stderr);
+ return EXIT_FAILURE;
+ }
- IList* set = ilist_new(0);
- IList* pending = ilist_new(argc);
- for (int i = 0; i < argc; i++)
- pending->items[i] = atoi(argv[i]);
+ IList* set = ilist_new(0);
+ IList* pending = ilist_new(argc);
+ for (int i = 0; i < argc; i++)
+ pending->items[i] = atoi(argv[i]);
- int max = 0;
- compute_pairs_max(&max, set, pending);
- printf("%d\n", max);
+ int max = 0;
+ compute_pairs_max(&max, set, pending);
+ printf("%d\n", max);
- ilist_free(set);
- ilist_free(pending);
+ ilist_free(set);
+ ilist_free(pending);
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
diff --git a/challenge-206/paulo-custodio/cpp/ch-1.cpp b/challenge-206/paulo-custodio/cpp/ch-1.cpp
index 4845728f67..c866da59c5 100644
--- a/challenge-206/paulo-custodio/cpp/ch-1.cpp
+++ b/challenge-206/paulo-custodio/cpp/ch-1.cpp
@@ -32,38 +32,38 @@ Output: 15
#include <vector>
int minutes(const std::string& str) {
- int hours = 0, minutes = 0;
- char sep;
- std::istringstream iss{ str };
- if ((iss >> hours >> sep >> minutes) && sep == ':')
- return hours * 60 + minutes;
- else
- return 0;
+ int hours = 0, minutes = 0;
+ char sep;
+ std::istringstream iss{ str };
+ if ((iss >> hours >> sep >> minutes) && sep == ':')
+ return hours * 60 + minutes;
+ else
+ return 0;
}
int main(int argc, char* argv[]) {
- argv++; argc--;
- if (argc < 2) {
- std::cerr << "Usage: ch-1 time..." << std::endl;
- return EXIT_FAILURE;
- }
-
- std::vector<int> items;
- for (int i = 0; i < argc; i++)
- items.push_back(minutes(argv[i]));
- std::sort(items.begin(), items.end());
- items.push_back(items.front() + 24 * 60);
-
- int min = items.back() - items.front();
- for (size_t i = 0; i < items.size() - 1; i++) {
- for (size_t j = i + 1; j < items.size(); j++) {
- int n = items[j] - items[i];
- if (n < min)
- min = n;
- }
- }
-
- std::cout << min << std::endl;
-
- return EXIT_SUCCESS;
+ argv++; argc--;
+ if (argc < 2) {
+ std::cerr << "Usage: ch-1 time..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> items;
+ for (int i = 0; i < argc; i++)
+ items.push_back(minutes(argv[i]));
+ std::sort(items.begin(), items.end());
+ items.push_back(items.front() + 24 * 60);
+
+ int min = items.back() - items.front();
+ for (size_t i = 0; i < items.size() - 1; i++) {
+ for (size_t j = i + 1; j < items.size(); j++) {
+ int n = items[j] - items[i];
+ if (n < min)
+ min = n;
+ }
+ }
+
+ std::cout << min << std::endl;
+
+ return EXIT_SUCCESS;
}
diff --git a/challenge-206/paulo-custodio/cpp/ch-2.cpp b/challenge-206/paulo-custodio/cpp/ch-2.cpp
index 07f944b3b5..d91460649d 100644
--- a/challenge-206/paulo-custodio/cpp/ch-2.cpp
+++ b/challenge-206/paulo-custodio/cpp/ch-2.cpp
@@ -36,50 +36,50 @@ So the maximum sum is 2.
#include <iostream>
#include <vector>
-void compute_pairs_max(int& max,
- const std::vector<int>& set, const std::vector<int>& pending
+void compute_pairs_max(int& max,
+ const std::vector<int>& set, const std::vector<int>& pending
) {
- if (pending.size() == 0) { // compute sum, set max
- int sum = 0;
- for (size_t i = 0; i < set.size(); i += 2) {
- int n = std::min(set[i], set[i + 1]);
- sum += n;
- }
- max = std::max(max, sum);
- }
- else { // recurse for each pair
- for (size_t i = 0; i < pending.size() - 1; i++) {
- for (size_t j = i+1; j < pending.size(); j++) {
- std::vector<int> new_set = set;
- std::vector<int> new_pending = pending;
-
- new_set.push_back(pending[i]);
- new_set.push_back(pending[j]);
-
- new_pending.erase(new_pending.begin() + j);
- new_pending.erase(new_pending.begin() + i);
-
- compute_pairs_max(max, new_set, new_pending);
- }
- }
- }
+ if (pending.size() == 0) { // compute sum, set max
+ int sum = 0;
+ for (size_t i = 0; i < set.size(); i += 2) {
+ int n = std::min(set[i], set[i + 1]);
+ sum += n;
+ }
+ max = std::max(max, sum);
+ }
+ else { // recurse for each pair
+ for (size_t i = 0; i < pending.size() - 1; i++) {
+ for (size_t j = i+1; j < pending.size(); j++) {
+ std::vector<int> new_set = set;
+ std::vector<int> new_pending = pending;
+
+ new_set.push_back(pending[i]);
+ new_set.push_back(pending[j]);
+
+ new_pending.erase(new_pending.begin() + j);
+ new_pending.erase(new_pending.begin() + i);
+
+ compute_pairs_max(max, new_set, new_pending);
+ }
+ }
+ }
}
int main(int argc, char* argv[]) {
- argv++; argc--;
- if (argc % 2 != 0) {
- std::cerr << "usage: ch-2 pairs..." << std::endl;
- return EXIT_FAILURE;
- }
-
- std::vector<int> set;
- std::vector<int> pending;
- for (int i = 0; i < argc; i++)
- pending.push_back(atoi(argv[i]));
-
- int max = 0;
- compute_pairs_max(max, set, pending);
- std::cout << max << std::endl;
-
- return EXIT_SUCCESS;
+ argv++; argc--;
+ if (argc % 2 != 0) {
+ std::cerr << "usage: ch-2 pairs..." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::vector<int> set;
+ std::vector<int> pending;
+ for (int i = 0; i < argc; i++)
+ pending.push_back(atoi(argv[i]));
+
+ int max = 0;
+ compute_pairs_max(max, set, pending);
+ std::cout << max << std::endl;
+
+ return EXIT_SUCCESS;
}
diff --git a/challenge-206/paulo-custodio/perl/ch-1.pl b/challenge-206/paulo-custodio/perl/ch-1.pl
index 8e8d8602e3..6d0bb7cf05 100644
--- a/challenge-206/paulo-custodio/perl/ch-1.pl
+++ b/challenge-206/paulo-custodio/perl/ch-1.pl
@@ -4,24 +4,24 @@
#
# Task 1: Shortest Time
# Submitted by: Mohammad S Anwar
-#
+#
# You are given a list of time points, at least 2, in the 24-hour clock format HH:MM.
-#
+#
# Write a script to find out the shortest time in minutes between any two time points.
# Example 1
-#
+#
# Input: @time = ("00:00", "23:55", "20:00")
# Output: 5
-#
+#
# Since the difference between "00:00" and "23:55" is the shortest (5 minutes).
-#
+#
# Example 2
-#
+#
# Input: @array = ("01:01", "00:50", "00:57")
# Output: 4
-#
+#
# Example 3
-#
+#
# Input: @array = ("10:10", "09:30", "09:00", "09:55")
# Output: 15
@@ -32,15 +32,15 @@ my @in = sort { $a <=> $b } map { minutes($_) } @ARGV;
push @in, $in[0] + minutes("24:00");
my $min = $in[-1] - $in[0];
for my $i (0..$#in-1) {
- for my $j ($i+1..$#in) {
- my $n = $in[$j] - $in[$i];
- $min = $n if $n < $min;
- }
+ for my $j ($i+1..$#in) {
+ my $n = $in[$j] - $in[$i];
+ $min = $n if $n < $min;
+ }
}
say $min;
-sub minutes {
- my($t) = @_;
- $t =~ /^(\d+):(\d+)$/ or die "invalid time $t\n";
- return $1*60+$2;
+sub minutes {
+ my($t) = @_;
+ $t =~ /^(\d+):(\d+)$/ or die "invalid time $t\n";
+ return $1*60+$2;
}
diff --git a/challenge-206/paulo-custodio/perl/ch-2.pl b/challenge-206/paulo-custodio/perl/ch-2.pl
index 17a17b0940..35f02b1fa9 100644
--- a/challenge-206/paulo-custodio/perl/ch-2.pl
+++ b/challenge-206/paulo-custodio/perl/ch-2.pl
@@ -4,32 +4,32 @@
#
# Task 2: Array Pairings
# Submitted by: Mohammad S Anwar
-#
+#
# You are given an array of integers having even number of elements..
-#
+#
# Write a script to find the maximum sum of the minimum of each pairs.
# Example 1
-#
+#
# Input: @array = (1,2,3,4)
# Output: 4
-#
+#
# Possible Pairings are as below:
# a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4
# b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3
# c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3
-#
+#
# So the maxium sum is 4.
-#
+#
# Example 2
-#
+#
# Input: @array = (0,2,1,3)
# Output: 2
-#
+#
# Possible Pairings are as below:
# a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1
# b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2
# c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1
-#
+#
# So the maximum sum is 2.
use Modern::Perl;
@@ -40,35 +40,35 @@ my @in = @ARGV;
my @pairs = all_pairs(@in);
my $max = 0;
for my $pair (@pairs) {
- my $sum = sum(map { min(@$_) } @$pair);
- $max = max($max, $sum);
+ my $sum = sum(map { min(@$_) } @$pair);
+ $max = max($max, $sum);
}
say $max;
sub all_pairs {
- my(@in) = @_;
- my @pairs;
- compute_pairs(\@pairs, [], [@in]);
- return @pairs;
+ my(@in) = @_;
+ my @pairs;
+ compute_pairs(\@pairs, [], [@in]);
+ return @pairs;
}
sub compute_pairs {
- my($pairs, $set, $pending) = @_;
- my @set = @$set;
- my @pending = @$pending;
- if (@pending) {
- for my $i (0..$#pending-1) {
- for my $j ($i+1..$#pending) {
- my @this_set = (@set, [$pending[$i], $pending[$j]]);
- my @this_pending = @pending;
- splice(@this_pending, $j, 1);
- splice(@this_pending, $i, 1);
- compute_pairs($pairs, \@this_set, \@this_pending);
- }
- }
- }
- else {
- push @$pairs, [@$set];
- }
+ my($pairs, $set, $pending) = @_;
+ my @set = @$set;
+ my @pending = @$pending;
+ if (@pending) {
+ for my $i (0..$#pending-1) {
+ for my $j ($i+1..$#pending) {
+ my @this_set = (@set, [$pending[$i], $pending[$j]]);
+ my @this_pending = @pending;
+ splice(@this_pending, $j, 1);
+ splice(@this_pending, $i, 1);
+ compute_pairs($pairs, \@this_set, \@this_pending);
+ }
+ }
+ }
+ else {
+ push @$pairs, [@$set];
+ }
}
-
+