perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 338 - Task 2: Max Distance

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-338/#TASK2
  3 #
  4 # Task 2: Max Distance
  5 # ====================
  6 #
  7 # You are given two integer arrays, @arr1 and @arr2.
  8 #
  9 # Write a script to find the maximum difference between any pair of values from both arrays.
 10 #
 11 ## Example 1
 12 ##
 13 ## Input: @arr1 = (4, 5, 7)
 14 ##        @arr2 = (9, 1, 3, 4)
 15 ## Output: 6
 16 ##
 17 ## With element $arr1[0] = 4
 18 ## | 4 - 9 | = 5
 19 ## | 4 - 1 | = 3
 20 ## | 4 - 3 | = 1
 21 ## | 4 - 4 | = 0
 22 ## max distance = 5
 23 ##
 24 ## With element $arr1[1] = 5
 25 ## | 5 - 9 | = 4
 26 ## | 5 - 1 | = 4
 27 ## | 5 - 3 | = 2
 28 ## | 5 - 4 | = 1
 29 ## max distance = 4
 30 ##
 31 ## With element $arr1[2] = 7
 32 ## | 7 - 9 | = 2
 33 ## | 7 - 1 | = 6
 34 ## | 7 - 3 | = 4
 35 ## | 7 - 4 | = 4
 36 ## max distance = 6
 37 ##
 38 ## max (5, 6, 6) = 6
 39 #
 40 #
 41 ## Example 2
 42 ##
 43 ## Input: @arr1 = (2, 3, 5, 4)
 44 ##        @arr2 = (3, 2, 5, 5, 8, 7)
 45 ## Output: 6
 46 ##
 47 ## With element $arr1[0] = 2
 48 ## | 2 - 3 | = 1
 49 ## | 2 - 2 | = 0
 50 ## | 2 - 5 | = 3
 51 ## | 2 - 5 | = 3
 52 ## | 2 - 8 | = 6
 53 ## | 2 - 7 | = 5
 54 ## max distance = 6
 55 ##
 56 ## With element $arr1[1] = 3
 57 ## | 3 - 3 | = 0
 58 ## | 3 - 2 | = 1
 59 ## | 3 - 5 | = 2
 60 ## | 3 - 5 | = 2
 61 ## | 3 - 8 | = 5
 62 ## | 3 - 7 | = 4
 63 ## max distance = 5
 64 ##
 65 ## With element $arr1[2] = 5
 66 ## | 5 - 3 | = 2
 67 ## | 5 - 2 | = 3
 68 ## | 5 - 5 | = 0
 69 ## | 5 - 5 | = 0
 70 ## | 5 - 8 | = 3
 71 ## | 5 - 7 | = 2
 72 ## max distance = 3
 73 ##
 74 ## With element $arr1[3] = 4
 75 ## | 4 - 3 | = 1
 76 ## | 4 - 2 | = 2
 77 ## | 4 - 5 | = 1
 78 ## | 4 - 5 | = 1
 79 ## | 4 - 8 | = 4
 80 ## | 4 - 7 | = 3
 81 ## max distance = 4
 82 ##
 83 ## max (5, 6, 3, 4) = 6
 84 #
 85 #
 86 ## Example 3
 87 ##
 88 ## Input: @arr1 = (2, 1, 11, 3)
 89 ##        @arr2 = (2, 5, 10, 2)
 90 ## Output: 9
 91 ##
 92 ## With element $arr1[0] = 2
 93 ## | 2 - 2  | = 0
 94 ## | 2 - 5  | = 3
 95 ## | 2 - 10 | = 8
 96 ## | 2 - 2  | = 0
 97 ## max distance = 8
 98 ##
 99 ## With element $arr1[1] = 1
100 ## | 1 - 2  | = 1
101 ## | 1 - 5  | = 4
102 ## | 1 - 10 | = 9
103 ## | 1 - 2  | = 1
104 ## max distance = 9
105 ##
106 ## With element $arr1[2] = 11
107 ## | 11 - 2  | = 9
108 ## | 11 - 5  | = 6
109 ## | 11 - 10 | = 1
110 ## | 11 - 2  | = 9
111 ## max distance = 9
112 ##
113 ## With element $arr1[3] = 3
114 ## | 3 - 2  | = 1
115 ## | 3 - 5  | = 2
116 ## | 3 - 10 | = 7
117 ## | 3 - 2  | = 1
118 ## max distance = 7
119 ##
120 ## max (8, 9, 9, 7) = 9
121 #
122 #
123 ## Example 4
124 ##
125 ## Input: @arr1 = (1, 2, 3)
126 ##        @arr2 = (3, 2, 1)
127 ## Output: 2
128 ##
129 ## With element $arr1[0] = 1
130 ## | 1 - 3 | = 2
131 ## | 1 - 2 | = 1
132 ## | 1 - 1 | = 0
133 ## max distance = 2
134 ##
135 ## With element $arr1[1] = 2
136 ## | 2 - 3 | = 1
137 ## | 2 - 2 | = 0
138 ## | 2 - 1 | = 1
139 ## max distance = 1
140 ##
141 ## With element $arr1[2] = 3
142 ## | 3 - 3 | = 0
143 ## | 3 - 2 | = 1
144 ## | 3 - 1 | = 2
145 ## max distance = 2
146 ##
147 ## max (2, 1, 2) = 2
148 #
149 #
150 ## Example 5
151 ##
152 ## Input: @arr1 = (1, 0, 2, 3)
153 ##        @arr2 = (5, 0)
154 ## Output: 5
155 ##
156 ## With element $arr1[0] = 1
157 ## | 1 - 5 | = 4
158 ## | 1 - 0 | = 1
159 ## max distance = 4
160 ##
161 ## With element $arr1[1] = 0
162 ## | 0 - 5 | = 5
163 ## | 0 - 0 | = 0
164 ## max distance = 5
165 ##
166 ## With element $arr1[2] = 2
167 ## | 2 - 5 | = 3
168 ## | 2 - 0 | = 2
169 ## max distance = 3
170 ##
171 ## With element $arr1[3] = 3
172 ## | 3 - 5 | = 2
173 ## | 3 - 0 | = 3
174 ## max distance = 3
175 ##
176 ## max (4, 5, 3, 3) = 5
177 #
178 ############################################################
179 ##
180 ## discussion
181 ##
182 ############################################################
183 #
184 # This one turns out to be two nested loops, calculating
185 # differences all the time, and keeping the maximum updated
186 # throughout.
187 
188 use v5.36;
189 
190 max_distance([4, 5, 7], [9, 1, 3, 4]);
191 max_distance([2, 3, 5, 4], [3, 2, 5, 5, 8, 7]);
192 max_distance([2, 1, 11, 3], [2, 5, 10, 2]);
193 max_distance([1, 2, 3], [3, 2, 1]);
194 max_distance([1, 0, 2, 3], [5, 0]);
195 
196 sub max_distance( $arr1, $arr2 ) {
197     say "Input: (" . join(", ", @$arr1) . "), (" . join(", ", @$arr2) . ")";
198     my $max = 0;
199     foreach my $i (@$arr1) {
200         foreach my $j (@$arr2) {
201             my $t = abs($i - $j);
202             $max = $t > $max ? $t : $max;
203         }
204     }
205     say "Output: $max";
206 }