RogerBW's Blog

The Weekly Challenge 243: Pairs on the Floor 19 November 2023

I’ve been doing the Weekly Challenges. The latest involved combinatorics and integer division. (Note that this ends today.)

Task 1: Reverse Pairs

You are given an array of integers.

Write a script to return the number of reverse pairs in the given array.

A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and b) nums[i] > 2 * nums[j]

I could have done a two-loop check, but it seemed more fun to use combinations; I wrote combination generators for the languages that don't have them back in 211 task 2 and 233 task 2. (But Dijkstra's algorithm, which I used when writing my own generators, returns the elements in reverse order, which meant some of the code ended up being written differently.)

Scala does technically have a combination generator, but it eliminates duplicates when an element is repeated, so rather than build a set of indices for it and then combine that I ported over my Kotlin code.

Perl:

sub reversepairs($a) {
  return scalar grep {$_->[0] > 2 * $_->[1]} combinations($a, 2);
}

Task 2: Floor Sum

You are given an array of positive integers (>=1).

Write a script to return the sum of floor(nums[i] / nums[j]) where 0 <= i,j < nums.length. The floor() function returns the integer part of the division.

This is very straightforward in languages that have strong typing, so that the default behaviour when dividing two integers is to return an integer. Or which have an integer division operator.

PostScript:

/floorsum {
    2 dict begin
    /a exch def
    0
    a {
        /iv exch def
        a { iv idiv } map { add } reduce add
    } forall
    end
} bind def

which is perhaps more readily understood in JavaScript:

function floorsum(a) {
    let n = 0;
    for (let iv of a) {
        for (let jv of a) {
            n += Math.floor(iv / jv);
        }
    }
    return n;
}

Full code on github.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1