Minimizing the sum of differences in a pair

You have two arrays, a and b

Both contain n elements, all positive and distinct.

you have to create a pair, by taking one number from each array, such that the sum of the differences of the pairs are minimized.

for example, a = [1, 2, 3], b = [4, 5, 6]

one possible set of pairs = (1, 4), (2, 5), (3, 6)

sum of differences = |1 – 4| + |2 – 5| + |3 – 6| = 9

And now the sum of the differences has to be as small as possible

Desing an algorithm that those this is O(nlogn) time

So far, I have sorted both these arrays. I am unsure how to proceed now

Check if a pair of vertices belongs to the min-cut

Given a digraph $ G = (V,A)$ with a source $ s \in V$ and a sink $ t \in V$ , I need to adapt the graph to known if a pair of vertices $ u \in V$ and $ v \in V$ belongs to the min-cut $ S$ between $ s$ and $ t$ . That is, a new vertex $ a$ belongs to the min-cut $ S$ if and only if $ u \in S$ and $ v \in S$ . This new vertex must preserve the original max-flow from $ s$ to $ t$ . I tried creating new arcs $ (u,a)$ and $ (a,v)$ with infinity capacities, but while $ a$ belongs to $ S$ when $ u \in S$ , this immediately forces $ v$ to belong to $ S$ also, which is not necessarily true if the max-flow was computed in the original graph $ G$ . So, is there a way to force a new node to be in the min-cut set $ S$ if a pair of vertices belongs to $ S$ ?

Finding sequence of pairs with second element of previous pair matching first element of next pair

I am interested in efficient ways of doing certain problem.

I have list of $ n$ pairs, where $ n$ is usually a few houndred thousands and each pair’s element is an integer (let’s assume it is integer from $ 0$ to $ 10000$ ) and I am trying to find a sequence such that it start and ends at chosen integer (we can assume it is eg. $ 0$ ) and second element of previous pair matches first element of next pair. So as an example, if we have set of pairs $ \{(0,1), (1,3), (3, 2) (3,0)\}$ the valid sequence would be eg. $ (0,1), (1,3), (3, 0)$ . If there is a few answers then I can find arbitrary one. Moreover it is no certain that my list of pairs actually has a solution. If it does not have solution, then the method should return no valid solutions.

I think that maybe some kind of dynamic programming could be useful here, but I don’t really have an idea for something better than just checking all the options, which I am almost certain is quite bad. Do you have any interesting insight about this problem?

Prove that if you pair arbitrarily pair up the elements of an array A of size n to get n/2 pairs,

then you discard pairs of unequal elements and keep just one from the pairs of matching elements. Then the resulting collection will have a majority if and only if A had a majority, i.e. there exists an element with more than floor(n/2) occurrences.

I am very confused about how to go about proving this. It is from a textbook DPV problem 2.23. I am trying to prove it but I end up disproving it.

I.e. Suppose we have an array of n elements A[], that has a majority of element x. that means A.count(x) > floor(n/2). Now suppose that if we add two different elements, [a, b] to array A, x is no longer the majority. Then: A.count(x) <= floor(n/2) + 1 -> A.count(x) = floor(n/2) + 1. But now if we apply the same procedure and pair [a, b] together, then by definition the resulting array should have a majority, even though the original [….] o [a, b] did not.

Key Pair permission on Windows 2019

I generated a key pair on Windows Server 2019 using the native ssh-keygen located in System32\OpenSSH. Now I think the permissions of the private key are somewhat worrying…

  1. Is it expected to have read-write-execute (700) for Administrators, and for the SFTPClient user (part of Users, not Administrators) on the Windows implementation?

  2. Is there any reason why the key isn’t following the same logic than on Unix/Linux? (POSIX permissions set to 600 = read-write for owner only)

enter image description here

enter image description here

Given a list of strings, find every pair $(x,y)$ where $x$ is a substring of $y$. Possible to do better than $O(n^2)$?

Consider the following algorithmic problem: Given a list of strings $ L = [s_1, s_2, \dots, s_n]$ , we want to know all pairs $ (x,y)$ where $ x$ is a substring of $ y$ . A trivial algorithm would be this:

foreach x in L:    foreach y in L:       if x is substring of y:          OUTPUT x,y 

However, this takes $ O(n^2)$ $ x$ substring of $ y$ operations – I am curious to know whether there is a faster algorithm?

How to authorize a subkey using a primary master key pair?

I’m using webcrypto, not PGP/GPG.

I would like to use a key pair to create a “subkey” that is authorized by my primary key in a way anyone can publicly verify so I don’t need to expose the primary key’s private component to any web-facing systems.

My idea is to:

  • primary key signs a hash of subkeys public component
  • then use the subkeys private key (proving it has access) to encrypt this signed blob

Verification would be:

  • use the subkey public key to decrypt the signature
  • use the primary key’s public to verify the signature.

Would this be safe? Do I need to ad any tamper protection (AHEAD/HMAC)? I’m interested in an answer for both ECC and RSA.