algorithms – Time efficient way to find pairs in an array whose sum equals a specific number?

Given an array of integers, we want to find how many explicit pairs can be made such that their sum is divisible by 60. The pairs do not need to be non-unique.

For example (29, 20, 30, 100, 30, 121, 160, 31, 20) has 6 pairs -> (29, 31) (20, 100) (20, 160) (30, 30) (100, 20) (160, 20)

The obvious and immediate thing is to make a nested for loop

 for (int i = 0; i < pairs.Count - 1; i++)
            for (int n = i + 1; n < pairs.Count; n++)
                    if ((pairs(i) + pairs(n)) % 60 == 0)

but that’s too slow. The above also doesn’t factor in duplicates like if the array had the number “30” as four elements, which is equal to a divergent series of (n-1)(n)/2 = (4-1)(4)/2 = 6.

How can we improve the performance?

My initial though is to make a dictionary/hashmap, but I am unsure how to iterate through that in a performant way.

regression – Is there more efficient way of writing up my model?

In my thesis I estimate the following linear regression model:

$$y_{t}= alpha+beta_{-1} N_{t+1}+beta_{1} N_{t}+beta_{2} N_{t-1}+beta_{3} sum_{k=3}^{4} N_{t-k} \+ beta_{4} sum_{k=5}^{7}N_{t-k} + beta_{5} sum_{k=8}^{10}N_{t-k}+ beta_{6} sum_{k=11}^{15}N_{t-k} + varepsilon_{t}$$

where $t$ is the time variable sampled at every five minutes; $X$ is the only independent variable considered; and index $t-k$ for $k>0$ ($k<0$) refers to lags (leads).
My question concerns the representation of the model in my paper: is there a more efficient way of formally writing up this regression model?
I am aware that if each lag had a unique parameter, I could simply write the model in terms of a sum of lagged, contemporaneous, and lagged X’s, i.e.
$y_{t}= alpha_+ sum_{k=-1}^{15} beta_{k} N_{t-k} +varepsilon_{t}$

Or is it possible to use a notation in terms of intervals as index: for e.g. $sum_{k=8}^{10}N_{t-k}: N_{(t-8,t-10)}$
I have never before encountered a research paper using this type of index, it might be completely incorrect.

Thank you in advance!

performance – Efficient loop over a integer range in Python

I want to search for positions inside a range of integers. How to improve the code in terms of efficiency, since both my search list and the ranges are very big:

#Range list with ids
range_lst = ((121000, 122000, 'foo'), (123456, 124557, 'bar'), (156789, 163659, 'egg'))
# Positions which we want to scan
pos = (123456, 369369)
# Neighbouring windows size
window_size = 10000

for p in pos:
    for r in range_lst:
        range_area = range(r(0), r(1))
        range_area_window = range(r(0)-window_size, r(1)+window_size)
        id = r(2)
        if p in range_area:
            print (p, id, 'exact hit!')
            # If we don't find an exact hit, we check the neighbouring region +- 10000
            if p in range_area_window:
                print (p, id, 'neighbour hit!')

forms – How to represent speed range values in a simple and efficient way?

I have a task to create a form for administrator of Google Maps Live Traffic Congestion feature. Here’s what needs to be input in the form :

  • Congestion level name and color for the map (green,yellow,red)
  • Road type
  • Speed range value

I attached the task in details as well as the things I did so far.
I would appreciate some feedback on overall form and advice on how to represent speed values since they can’t overlap.

enter image description here
enter image description here

backup – how to migrate a mongodb over a couple of versions safely & efficient?

what is the best method to migrate a database (or at least 1 of 6 collections) from mongodb 2.4 to 4.4 with “reasonable effort”?

  • i know there is mongodump/mongorestore, which is considered the “more accurate” method and is advised for backup & restore on the one hand…
    but as far as i’ve read, the mongodb versions must be the same and they can only be upgraded one after another! so i would have to upgrade 8 times (2.4 -> 2.6 -> 3.0 -> 3.2 -> 3.4 -> 3.6 -> 4.0 -> 4.2 -> 4.4) manually, which is pretty tedious; not to mention, that not all versions are available for the old machine (Debian 8 with SysVinit).
    btw: i tried to install mongodb 4.4 on Debian 10 with SysVinit, which failed because of systemd missing (no, not just starting the service, indeed the installation)! 🙁
    –> if upgrading to every version takes as much fiddling around & time as installing mongodb 4.4 on Debian 10 with SysVinit did, the whole process would take up to a week, which is insane!

  • but on the other hand there is mongoexport/mongoimport which is more or less only the tool of choice, if a 3rd party is involved, because it can cause data loss.
    is it nevertheless worth a try in this situation? how can i check for a data loss resp. avoid or even better be sure, that there is no data loss? is using mongoexport --jsonFormat=canonical (...) & mongoimport --stopOnError (...) and then comparing the number of exported & imported records sufficient, to be on the safe side?

or is even using a dump from mongodb 2.4 to restore at mongodb 4.4 worth a try, despite all docs and tutorials? how can i check easily for data loss resp. messed up data in this case?

to mention it, just in case: i’m kind of an advanced linux user, but by no means a database expert! so please bear with me, if i’ve got something wrong.

java – Efficient method for Sprite Stacking in LibGDX

What would be a very efficient method for creating a suede 3D effect by stacking sprites much like the game like NIUM in LibGDX

I find it easy to create a rudimentary system that stacks textures in a sprite batch and rotate them accordingly but a system where each texture has to have it’s rotation updated and then drawn with the sprite batch isn’t a very efficient way to create such an effect. It tends to struggle for larger scenes and for large objects with many sprites to create this effect seems to bring it to its knees.

combinatorics – Efficient algorithm for this combinatorial problem


I am working on a combinatorial optimization problem and I need to figure out a way to solve the following equation. It naturally popped up in a method I chose to use in my assignment I was working on.

Given a fixed set $Theta$ with each element $in (0,1)$ and total $N$ elements ($N$ is about 25), I need to find a permutation of elements in $Theta$ such that
$$ vec K = argmin_{vec k = Permutation(Theta)} sum_{i=1}^N t_i D(mu_i||k_i) $$
where $vec t, vec mu$ are given vectors of length $N$ and $D(p||q)$ is the KL Divergence of the bernoulli distributions with parameters $p$ and $q$ respectively. Further, all the $N$ elements of $vec t$ sum to 1 and $vec mu$ has all elements in $(0,1)$.

It is just impossible to go through all $N!$ permutations. A greedy type of algorithm which does not give exact $vec K$ would also be acceptable to me if there is no other apparent method. Please let me know how to proceed!

algorithms – Efficient way to find Xor of pairs with given target X in a range 0 to K

I came across a problem: given an array (can be huge 10^8) and an integer, K, find the count of pairs in the array whose Xor equal to each value in the range 0 to K.

 Arr = 1 2 3
 K = 2 

since K=2, possible values of x are 0, 1 or 2.

x = 0

  • There are 0 pairs whose Xor=0.

x = 1

  • There is 1 pair {2,3}, whose Xor=1.

x = 2

  • There is 1 pair {1,3}, whose Xor=2.

So output is

0 1 1

I know an n² solution to this problem where I loop x from 0 to K and now used hashing over the array find all such pairs whose sum is equal to the current x add it to the result array.

I’m seeking a better approach.

python – Is the following divide and concur recursive algorithm for the exponentiation more efficient than the iterative one for large numbers?

I have the two following algorithms. My analysis says that both of them are $mathcal O(m^2+4^n)$ i.e they are equivalent for big numbers. Is this right. Note that m and n are the bits numbers for x and y

def pow1(x, y):

    if y == 0:
        return 1
    temp = x
    while y > 1:
        y -= 1
        temp *= x
    return temp

def pow2(x, y):

    if y == 0:
        return 1
    temp = pow2(x, y//2)
    if y & 1:    return temp * x
    return temp

Freelance article writer with great quality content for $50

Select a reason you’re reporting this service