Two-Sum – Range Allowance Algorithm Design

🧩 How to optimally find a two-sum solution within a range plus/minus the given capacity?

Two-Sum: Determine whether there are two items whose length will equal the total length while ensuring the same item cannot be used twice. This optimizes for runtime over memory.

  • Time complexity: $O(n)$
  • Space complexity: $O(n)$

Samples

Input: (4, 5, 2, 6)

  • Total length: 10
  • Expect: true

Input: (4, 5, 2, 5)

  • Total length: 10
  • Expect: true

Input: (4, 5, 2, 7)

  • Total length: 10
  • Expect: false

Code

fun isLengthMatch(totalLength: Int, lengths: IntArray): Boolean {
    handleErrors(totalLength, lengths)
    val searchSet = hashSetOf<Int>()
    for (length in lengths) {
        val targetLength = totalLength - length
        if (searchSet.contains(targetLength)) return true
        else searchSet.add(length)
    }
    return false
}

fun handleErrors(totalLength: Int, lengths: IntArray) {
    if (totalLength <= 0) throw IllegalArgumentException(""totalLength" must be greater than 0.")
    if (lengths.size < 2) throw IllegalArgumentException(""lengths" cannot be less than two items.")
}
  1. Generate a targetLengthsArray +/- the totalLength and the given range allowance.

  2. Check if the searchSet contains any of the values in the targetLengthsArray.

microsoft excel – Use table column names as range in INDEX() function

In an Excel 2016 table:

I have a formula that I use to check if parent records have the right ‘Use With’ values (if a child record has a ‘Use With’ value, then it’s parents must have it too). More info here.

Column B =

IFERROR(IF(SUMPRODUCT(COUNTIF(INDEX( C:E, (@(Parent - RowNum)),0),Table1(@(Use With 1):(Use With 3))))<>COUNTA(Table1(@(Use With 1):(Use With 3))), "error", ""),"")

enter image description here


For example, if I were to delete the value in C2, the formula would successfully flag it as causing an error:

enter image description here


Question:

I’m trying to convert all explicit cell references —to— structured references (aka table column names). I want to do this to avoid some issues I’ve been having with adding/deleting columns in the spreadsheet (and because I assume it’s best practice/cleaner).

I’ve tried to replace C:E with Table1((Use With 1):(Use With 3)).

New formula for column B:

=IFERROR(IF(SUMPRODUCT(COUNTIF(INDEX( Table1((Use With 1):(Use With 3)), (@(Parent - RowNum)),0),Table1(@(Use With 1):(Use With 3))))<>COUNTA(Table1(@(Use With 1):(Use With 3))), "error", ""),"")

However, when I do that, the formula fails to work correctly — it doesn’t flag the problem rows with “error”.

enter image description here


What’s the correct way to use table column names as a range in the INDEX() function (instead of using explicit cell references)?

networking – How to only allow routing to IP range on particular interface?

I have two interfaces eth0 (192.168.10.x) and wlan0 (172.16.30.x) on my device (Raspberry Pi, Raspbian GNU/Linux 10 (buster)).

Both networks have access to the internet, but I want my device to only use the eth0 for any IP ranges apart from the 172.16.30.x range which should only go via the wlan0.

(Why? I’m trying to segregate messages on IoT WiFi network from my “safe” one – please don’t suggest a router solution, that’s not possible for other reasons.)

So

172.16.30.x      -->  wlan0
everything else  -->  eth0

I also want the changes to persist after reboots.

I assumed that I should use the route command, but failed in understanding it.

I’ve tried the following in ufw and it blocks all packets (well, I’m testing with ping) on the wlan0 interface.

sudo ufw route allow out on wlan0 to 172.16.30.0/24
sudo ufw deny out on wlan0

Can someone help please?

ssh – ssh_dispatch_run_fatal / DH GEX group out of range

I’m running Ubuntu 20.04 / OpenSSH_8.2p1 and have a client trying to connect but it’s failing with error in auth.log like:

Nov 25 02:13:20 sshd[517621]: ssh_dispatch_run_fatal: Connection from 192.0.2.1 port 50127: DH GEX group out of range [preauth]

I did see a few other questions asking about this error:

https://serverfault.com/questions/786587/dh-gex-group-out-of-range/809082
https://serverfault.com/questions/729033/ssh-dh-gex-group-out-of-range
https://serverfault.com/questions/852666/sftp-connection-fails-with-dh-gex-group-out-of-range
https://serverfault.com/questions/920839/ssh-error-ssh-dispatch-run-fatal-connection-to-192-168-x-x-port-22-dh-gex-gro

But all of those questions relate to someone with a newer client trying to connect to an older server and receiving the error on the client end. The best solution in this case is probably to upgrade the client. In my case however, I have no control over the client but I’d like our server to accept their connections. Is there a directive I can adjust in /etc/ssh/sshd_config to allow older clients to connect? Or any other solution that doesn’t involve asking the client to upgrade their version?

dnd 5e – What does “no long range” mean on the soulknife rogue subclass mean?

One of my players just hit level 3 and is taking the soulknife rogue subclass, as written in Tasha’s Cauldron of Everything.

The weapon granted by the class has a weird specification that it has “no long range.” I’m curious how this should be interpreted, seeing as I can’t find other examples of such a property.

This is what Tasha’s says:

You can manifest your psionic power as shimmering blades of psychic energy. Whenever you take the Attack action, you can manifest a psychic blade from your free hand and make the attack with that blade. This magic blade is a simple melee weapon with the finesse and thrown properties. It has a normal range of 60 feet and no long range, and on a hit, it deals psychic damage equal to 1d6 plus the ability modifier you used for the attack roll. The blade vanishes immediately after it hits or misses its target, and it leaves no mark on its target if it deals damage.

There are two possibilities in my mind:

  1. The knife simply can’t go beyond 60 feet, just like the range of a ranged spell attack.

  2. The knife can go arbitrarily far, and after 60 feet the attack is at disadvantage.

I’m inclined to lean for the first choice since an infinitely long ranged attack seems dumb. Though I suppose there’s an argument to be made that the manifested blade is a “psychic attack” so you can attack someone so long as you can see them.

This is the language of ranged weapon attacks, per the PHB:

A weapon that can be used to make a ranged Attack has a range in parentheses after the Ammunition or thrown property. The range lists two numbers. The first is the weapon’s normal range in feet, and the second indicates the weapon’s long range. When attacking a target beyond normal range, you have disadvantage on the Attack roll. You can’t Attack a target beyond the weapon’s long range.

google sheets – Adding range into code to get the range PDF

The code was working perfectly before adding this code > .getRange(“A1:K33”).activate() < the purpose of adding this line is to get the pdf of the given range.

If i remove the get range from the code it will work perfectly and save the whole sheet PDF into drive.

But i am trying to add a range into code that it will just make a pdf of desired range.

For example if i want A1:J50 range pdf then by mentioning that range into code and after running the code it should save the pdf of specific range.

Looking for any help.

function myFunction() {
const ss = SpreadsheetApp.openByUrl("Url");
  const sheet = ss.getSheetByName("PaySlip").getRange("A1:K33").activate();
const ssID = ss.getId();
const shID = sheet.getSheetId().toString();
const nameFile = sheet.getRange("E7").getValue().toString() +".pdf";
const fid = 'Url ID';  
const folder = DriveApp.getFolderById(fid);  
const requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};  
const url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=pdf&id="+ssID+"&gid="+shID;
const result = UrlFetchApp.fetch(url , requestData);  
folder.createFile(result.getBlob()).setName(nameFile);  
}

algorithms – Design a data structure with constant insertion and sublinear range maximum query

Suppose we have access to a stream of ordered pairs, where the $i^{text{th}}$ element in the stream is an ordered pair $(a_i, b_i)$, where $a_i$ and $b_i$ are both arbitrary reals. How can we design a data structure with the following two methods:

  1. $mathcal{O}(1)$ insert
  2. Sublinear lookup to determine when, given some real $a$ as an argument, find the maximum value of $b_i$ for all elements $(a_i, b_i)$ already in our data structure with $a_i$ in the range $(a-1, a)$.

Note: The $a_i$‘s are not necessarily in sorted order in the stream.

infrared – Extend PIR range by adding a lens?

I bought this solar powered wifi camera. I stuck it in a tree above my front gate, hoping to automatically trigger to record vehicles and people in front of it. The footage uploads to the web, via my wifi network.

Whilst the video quality is fine and uploading works, the PIR detection range is inadequate. They claim 10 meters, but I reckon its less than that.

Would it be straightforward to extend the PIR range to say 25 or 30 meters by sticking a lens in front of what’s there already (a fresnel lense)?

I understand the extra lens would need to be transparent to infrared.

dnd 5e – What is the difference between a spell with a range of “Self” and a spell with a range of “Self (XYZ)?

The ranges “Self” and “Self (XYZ)” are different

The Player’s Handbook (p202) gives us two definitions of the range of Self in the Range section of the Spellcasting chapter:

Most spells have ranges expressed in feet. Some spells can target only a creature (including you) that you touch. Other spells, such as the shield spell, affect only you. These spells have a range of self.

Spells that create cones or lines of effect that originate from you also have a range of self, indicating that the origin point of the spell’s effect must be you (see “Areas of Effect” later in the this chapter).

So, we have two definitions of Self, and since D&D 5e is governed by the Specific vs General rule, both are valid:

  • “Self”: means that the spell targets the caster
  • “Self (XYZ)”: means that the spell is an area of effect spell, whose point of origin must be the caster

So, if a spell has a Range of “Self (XYZ)” the caster is not a target of the spell, unless the spell’s description includes them as a targeted creature (by being affected by the spell).