The Weekly Challenge 251

Concatenation Value & Lucky Numbers

Official Challenge • My Solutions on GitHub

Task 1: Concatenation Value

You are given an array of integers, @ints. Write a script to find the concatenation value of the given array. The concatenation of two numbers is the number formed by concatenating their numerals.

Starting from 0, repeatedly pick the first and last element, add their concatenation to the total, and remove both elements. If a single element remains, add its value directly and remove it.

Examples

Input: @ints = (6, 12, 25, 1) Output: 1286 # 1st operation: 6 || 1 = 61 # 2nd operation: 12 || 25 = 1225 # Total: 61 + 1225 = 1286 Input: @ints = (10, 7, 31, 5, 2, 2) Output: 489 # 102 + 72 + 315 = 489 Input: @ints = (1, 2, 10) Output: 112 # 110 + 2 = 112

Algorithm & Approach

Instead of mutating the input array by repeatedly shifting and popping elements, we use a two-pointer scan with indices left = 0 and right = n - 1:

This runs in O(n) time and O(1) auxiliary space without modifying the caller's input.

Perl Solution

#!/usr/bin/env perl
use v5.38;
use warnings;
use Type::Params -sigs;
use Types::Standard qw(ArrayRef Int);
use Test::More;

signature_for concatenation_value => (
    method     => 0,
    positional => [ ArrayRef [Int] ],
);

sub concatenation_value ($ints) {
    my $concat_value = 0;
    my $left         = 0;
    my $right        = $#{$ints};

    while ( $left < $right ) {
        $concat_value += int( "$ints->[$left]$ints->[$right]" );
        $left++;
        $right--;
    }

    if ( $left == $right ) {
        $concat_value += $ints->[$left];
    }

    return $concat_value;
}

Python Solution

#!/usr/bin/env python3
"""Concatenation Value - Perl Weekly Challenge 251, Task 1."""

from __future__ import annotations

import unittest


def concatenation_value(ints: list[int]) -> int:
    """Return the concatenation value of the given list of integers."""
    concat_value = 0
    left = 0
    right = len(ints) - 1

    while left < right:
        concat_value += int(f"{ints[left]}{ints[right]}")
        left += 1
        right -= 1

    if left == right:
        concat_value += ints[left]

    return concat_value

Task 2: Lucky Numbers

You are given an m x n matrix of distinct numbers. Write a script to return the lucky number, if there is one, or -1 if not. A lucky number is an element of the matrix such that it is the minimum element in its row and the maximum element in its column.

Examples

Input: $matrix = [ [ 3, 7, 8], [ 9, 11, 13], [15, 16, 17] ]; Output: 15 Input: $matrix = [ [ 1, 10, 4, 2], [ 9, 3, 8, 7], [15, 16, 17, 12] ]; Output: 12 Input: $matrix = [ [7, 8], [1, 2] ]; Output: 7

Algorithm & Approach

Because all numbers in the matrix are distinct, each row has a unique minimum element. For each row:

Time complexity is O(m · (n + m)) and space complexity is O(1) extra space.

Perl Solution

#!/usr/bin/env perl
use v5.38;
use warnings;
use List::Util qw(max min);
use Type::Params -sigs;
use Types::Standard qw(ArrayRef Int);
use Test::More;

signature_for lucky_number => (
    method     => 0,
    positional => [ ArrayRef [ ArrayRef [Int] ] ],
);

sub lucky_number ($matrix) {
    for my $row ( @{$matrix} ) {
        next if !@{$row};
        my $min_row   = min( @{$row} );
        my ($min_idx) = grep { $row->[$_] == $min_row } 0 .. $#{$row};
        my $max_col   = max( map { $_->[$min_idx] } @{$matrix} );
        return $min_row if $min_row == $max_col;
    }
    return -1;
}

Python Solution

#!/usr/bin/env python3
"""Lucky Numbers - Perl Weekly Challenge 251, Task 2."""

from __future__ import annotations

import unittest


def lucky_number(matrix: list[list[int]]) -> int:
    """Return the lucky number in matrix, or -1 if none exists.

    A lucky number is an element that is the minimum in its row
    and the maximum in its column.
    """
    for row in matrix:
        if not row:
            continue
        min_row = min(row)
        min_index = row.index(min_row)
        if all(other_row[min_index] <= min_row for other_row in matrix):
            return min_row
    return -1