| 30 October 2026 | Challenge 397 |
Absent Reflections
Task 1: Absent Smallest Positive
Submitted by: Mohammad Sajid Anwar
You are given an array of integers.
Write a script to return the smallest absent positive integer such that it is strictly greater than the average of all elements in the given array.
Example 1
Input: @arr = (1, 2, 4, 5, 6)
Output: 7
Average: (1 + 2 + 4 + 5 + 6) / 5 = 3.6
We need the smallest positive integer absent from nums that is strictly greater than 3.6.
The positive integers greater than 3.6 are 4, 5, 6, 7, ....
4 is in the given array.
5 is in the given array.
6 is in the given array.
7 is not in.
Example 2
Input: @arr = (-5, -2, -1, 3)
Output: 1
Average: (-5 -2 -1 + 3) / 4 = -1.25
We need the smallest absent positive integer strictly greater than -1.25.
Positive integers are 1, 2, 3, 4, ... (all are strictly greater than -1.25).
1 is positive and absent from the given array.
Example 3
Input: @arr = (1, 2, 3)
Output: 4
Average: (1 + 2 + 3) / 3 = 2.0
We need an integer strictly greater than 2.0.
3 is in the given array.
4 is not in.
Example 4
Input: @arr = (2, 3, 7, 8)
Output: 6
Average: (2 + 3 + 7 + 8) / 4 = 5.0
We need an integer strictly greater than 5.0.
6 is not in the given array.
Example 5
Input: @arr = (1, 1, 1, 10)
Output: 4
Average: (1 + 1 + 1 + 10) / 4 = 3.25
Positive integers strictly greater than 3.25: 4, 5, 6, ...
4 is not in the given array.
Solution
Implementing a solution is straightforward:
Use the smallest positive integer above average as the starting value of an infinite loop
and break the loop on the first value missing from the list.
Perl
use strict;
use warnings;
use List::Util qw(max sum);
sub spaaa {
(\my %arr)->@{@_} = ();
for (my $x = 1 + max 0, int sum(@_) / @_;; $x++) {
exists $arr{$x} || return $x
}
}
See the full solution to task 1.
J
spaaa =: _(adverb define)
int_avg =. 0 >. [: <. +/ % #
find_missing =. {{
y =. >: y
y (1 Z: -.@e.) x
y
}}
([ (] F. find_missing) int_avg) f.
)
See the full solution.
Task 2: Binary Reflection
Submitted by: Mohammad Sajid Anwar
You are given an array of integer.
Write a script to sort the given array in ascending order based on the binary reflection of each element. If two different numbers have the same binary reflection, the smaller original number should appear first.
Example 1
Input: @arr = (3, 6, 12)
Output: (3, 6, 12)
Decimal 3 = Binary 11 -> reverse is also same 11 = Decimal 3
Decimal 6 = Binary 110 -> reverse 011 = Decimal 3
Decimal 12 = Binary 1100 -> 0011 = Decimal 3
All three elements have the same binary reflection (3).
Since they tie, they are sorted by their original values in ascending order: (3, 6, 12).
Example 2
Input: @arr = (1, 2, 4, 8)
Output: (1, 2, 4, 8)
Decimal 1 = Binary 1 -> reverse is also same 1 = Decimal 1
Decimal 2 = Binary 10 -> reverse is 01 = Decimal 1
Decimal 4 = Binary 100 -> reverse 001 = Decimal 1
Decimal 8 = Binary 1000 -> reverse is 0001 = Decimal 1
All elements reflect to 1. Tie-breaking by original value yields (1, 2, 4, 8).
Example 3
Input: @arr = (7, 8, 9, 10)
Output: (8, 10, 7, 9)
Decimal 8 = Binary 1000 -> reverse 0001 = Decimal 1
Decimal 9 = Binary 1001 -> reverse 1001 = Decimal 9
Decimal 10 = Binary 1010 -> reverse 0101 = Decimal 5
Decimal 7 = Binary 111 -> reverse 111 = Decimal 7
Binary reflections:
8 -> 1
9 -> 9
10 -> 5
7 -> 7
Sorting by reflection values (1, 5, 7, 9) gives original elements (8, 10, 7, 9).
Example 4
Input: @arr = (5, 9, 15, 17)
Output: (5, 9, 15, 17)
Decimal 5 = Binary 101 -> reverse 101 = Decimal 5
Decimal 9 = Binary 1001 -> reverse 1001 = Decimal 9
Decimal 15 = Binary 1111 -> reverse 1111 = Decimal 15
Decimal 17 = Binary 10001 -> reverse 10001 = Decimal 17
All numbers are binary palindromes, so their reflections match their original decimal values (5, 9, 15, 17),
leaving the array order unchanged.
Example 5
Input: @arr = (11, 13, 14, 19)
Output: (14, 13, 11, 19)
Decimal 11 = Binary 1011 -> reverse 1101 = Decimal 13
Decimal 13 = Binary 1101 -> reverse 1011 = Decimal 11
Decimal 14 = Binary 1110 -> reverse 0111 = Decimal 7
Decimal 19 = Binary 10011 -> reverse 11001 = Decimal 25
Reflections:
14 -> 7
13 -> 11
11 -> 13
19 -> 25
Sorted by reflection values (7, 11, 13, 25) yields (14, 13, 11, 19).
Solution
Sort by reflected binary and the value itself.
Perl
I already had written a solution based on a Schwartzian transform, when I came across
Sort::Key in a very interesting Perlmonks’
thread.
Here the required function bin_rx_sort is generated by a use statement and can be directly applied to an array.
use strict;
use warnings;
use Math::Prime::Util qw(fromdigits todigits);
use Sort::Key::Maker bin_rx_sort => sub {
(fromdigits([reverse todigits $_, 2], 2), $_)
}, 'int', 'int';
See the full solution to task 2.
J
More similar to a Schwartzian transform, it generates the sort keys by combining the reflection values with their originals.
The “under” conjunction &. simplifies the binary reflection operation.
- perform “reverse under conversion to base 2” (
|.&.#:) - “stitch” to original values (
,.) - use the result as the keys to sort (
/:) the original values - enforce monadic usage
bin_rx =: ] /: |.&.#:"0 ,. ] : [: bin_rx 7 8 4 2 1 1 2 4 8 7See the full solution.