Monday, October 27, 2025

TWC345

Challenge Link

Task1

We traverse the array by steps of 3 elements each time and test for the peak element:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub peak_positions{
  my ($arr) = @_;
  my @res;
  foreach my $i(1..$#$arr) {
    if($arr->[$i-1] < $arr->[$i] && $arr->[$i+1] < $arr->[$i]) {
      push @res,$i
    }
  }
  @res
}

show peak_positions([1,3,2]);
show peak_positions([2,4,6,5,3]);
show peak_positions([1,2,3,2,4,1]);
show peak_positions([5,3,1]);
show peak_positions([1,5,1,5,1,5,1]);

Task2

We simulate the operations while traversing the array:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub last_visitor{
  my ($arr) = @_;
  my (@res,@seen);
  my $k = 0;
  foreach my $i(0..$#$arr) {
    if($arr->[$i] != -1) {
      push @seen,$arr->[$i];
      $k = @seen
    } elsif ($k == 0) {
      push @res,-1
    } else {
      push @res,$seen[--$k]
    }
  }
  @res
}

show last_visitor([5,-1,-1]);
show last_visitor([3,7,-1,-1,-1]);
show last_visitor([2,-1,4,-1,-1]);
show last_visitor([10,20,-1,30,-1,-1]);
show last_visitor([-1,-1,5,-1])

Tuesday, October 21, 2025

TWC344

Challenge Link

My Perl somehow got broken, and I didn't yet have the time to fix the issue, so this post will contain Java solutions.

Task1

We add the digit x to the array which contains the digits of a number:
import java.util.List;
import java.util.LinkedList;

public class Ch1 {
  public static void main(String[] args) {
    System.out.println(array_form_compute(new int[]{1,2,3,4},12));
    System.out.println(array_form_compute(new int[]{2,7,4},181));
    System.out.println(array_form_compute(new int[]{9,9,9},1));
    System.out.println(array_form_compute(new int[]{1,0,0,0,0},9999));
    System.out.println(array_form_compute(new int[]{0},1000));
  }

  private static List array_form_compute(int[] arr,int k) {
    int i = arr.length-1,carry = 0;
    List res = new LinkedList<>();
    while(i >= 0 || k > 0 || carry > 0) {
      carry += (i < 0 ? 0 : arr[i--]) + k % 10;
      res.addFirst(carry % 10);
      carry /= 10;
      k /= 10;
    }
    return res;
  }
}

Task2

We check if the array can be formed by combining the source elements:
public class Ch2 {
  public static void main(String[] args) {
    System.out.println(array_formation(new int[][]{{2,3},{1},{4}},
				       new int[]{1,2,3,4}));
    System.out.println(array_formation(new int[][]{{1,3},{2,4}},
				       new int[]{1,2,3,4}));
    System.out.println(array_formation(new int[][]{{9,1},{5,8},{2}},
				       new int[]{5,8,2,9,1}));
    System.out.println(array_formation(new int[][]{{1},{3}},
				       new int[]{1,2,3}));
    System.out.println(array_formation(new int[][]{{7,4,6}},
				       new int[]{7,4,6}));
  }

  private static boolean array_formation(int[][]source,
					 int[] target) {
    for(int i = 0; i < target.length;) {
      int k = 0;
      while(k < source.length && source[k][0] != target[i]) ++k;
      if(k == source.length) return false;
      int j = 0;
      while(j < source[k].length && target[i] == source[k][j]) {
	++i;
	++j;
      }
    }
    return true;
  }
}

Monday, October 13, 2025

TWC343

Challenge Link

Task1

We find the number with the smallest distance from zero:
#!/usr/bin/env perl
use strict;
use warnings;

sub zero_friend{
  my $zf = abs shift @{$_[0]};
  abs $_ < $zf and $zf = abs for @{$_[0]};
  $zf
}

printf "%d\n",zero_friend([4,2,-1,3,-2]);
printf "%d\n",zero_friend([-5,5,-3,3,-1,1]);
printf "%d\n",zero_friend([7,-3,0,2,-8]);
printf "%d\n",zero_friend([-2,-5,-1,-8]);
printf "%d\n",zero_friend([-2,2,-4,4,-1,1]);

Task2

We count the wins and find the champion team:
#!/usr/bin/env perl
use strict;
use warnings;

sub champion_team{
  my $m = 0;
  my %h;
  foreach my $y(0..$#{$_[0]}){
    foreach my $x(0..$#{$_[0]->[$y]}){
      my $w = $_[0]->[$y][$x] ? $y : $x;
      $m = $w if ++$h{$w} > $h{$m}
    }
  }
  $m
}

printf "%d\n",champion_team([[0, 1, 1],
			     [0, 0, 1],
			     [0, 0, 0]]);
printf "%d\n",champion_team([[0, 1, 0, 0],
			     [0, 0, 0, 0],
			     [1, 1, 0, 0],
			     [1, 1, 1, 0]]);
printf "%d\n",champion_team([[0, 1, 0, 1],
			     [0, 0, 1, 1],
			     [1, 0, 0, 0],
			     [0, 0, 1, 0]]);
printf "%d\n",champion_team([[0, 1, 1],
			     [0, 0, 0],
			     [0, 1, 0]]);
printf "%d\n",champion_team([[0, 0, 0, 0, 0],
			     [1, 0, 0, 0, 0],
			     [1, 1, 0, 1, 1],
			     [1, 1, 0, 0, 0],
			     [1, 1, 0, 1, 0]]);

Monday, October 6, 2025

TWC342

Challenge Link

Task1

We keep on alternating between digits and letters:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(min);

sub balance_string{
  my @chars = split '',$_[0];
  my @ds = grep{/\d/} @chars;
  my @ls = grep{/\D/} @chars;
  my ($m,$n) = (scalar @ds,scalar @ls);
  return '' if abs($m-$n) > 1;
  my $res;
  foreach my $i(0..min($m,$n)-1){
    if($m > $n) {
      $res .= $ds[$i] . $ls[$i]
    } else {
      $res .= $ds[$i] . $ls[$i]
    }
  }
  $res .= $ds[$m-1] if($m > $n);
  $res .= $ls[$n-1] if($m < $n);
  $res
}

printf "%s\n",balance_string('a0b1c2');
printf "%s\n",balance_string('abc12');
printf "%s\n",balance_string('0a2b1c3');
printf "%s\n",balance_string('1a23');
printf "%s\n",balance_string('ab123')

Task2

We find the max score by prefix sum:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub max_score{
  my $res = 0;
  foreach my $i(1..(length $_[0])-1) {
    my $temp = 0;
    foreach my $j(0..$i-1) {
      $temp++ if (substr $_[0],$j,1) eq '0'
    }
    foreach my $j($i..(length $_[0])-1) {
      $temp++ if (substr $_[0],$j,1) eq '1'
    }
    $res = max $res,$temp;
  }
  $res
}

printf "%d\n",max_score('0011');
printf "%d\n",max_score('0000');
printf "%d\n",max_score('1111');
printf "%d\n",max_score('0101');
printf "%d\n",max_score('011101')