Effective algorithm for finding the optimal sum of both arrays

I have two tables (A, B) and a sum M. I need to find a number from each table that does sum M. If there are not two items which form the sum M, then the nearest solution and the smallest are the sum. See the examples below. What is the effective way to find the solution for this?

A =(200,300,600,900)
B =(1000,200,300,500)
M = 900
Output : A(2), B(2)
600 from A and 300 from B sums 900.

A =(200,300,600,1000)
B =(1000,400,600)
M = 1500
Output : A(3), B(1)
1000 from A and 400 from B sums 1400 which is near to the sum 1500.

How can I avoid recalculating Dijkstra's algorithm if I add or delete a graphic edge?

You can calculate at the beginning a complete SSSP (Single Source Shortest Path) path for your two vertices (s and t).

Then, each time you add an edge, you update only the shortest paths affected, i. e. you check if the uv edge provides an improvement for sv, su, tv or you. Otherwise, your SSSP is always correct. If this gives you an improvement, you should also check the neighbors for updates. This stunts the same way as the Dijkstra algorithm.

I can not propose an asymptotic limit for the moment, but if the graph and the insertion order are not chosen adversely, a single edge should not often invoke a change in large parts of the graph (and when it happens, duty recalculate anyway).

game theory – Why does the algorithm Min-Max delay indefinitely a good shot?

I've implemented a min-max algorithm for a game and discovered a problem.

Let's say that there is a 2×2 grid:


Suppose we start at the location (0, 0) and the target is (1, 1):


Permitted movements: adjacent or diagonal movements.

Evaluation: if I landed at (1, 1), I win +100.

If I implement the min-max algorithm with depth 1, some of the branches are as follows:

(0, 0) -> (0, 1) -> (1, 1) We are returning here +100

(0, 0) -> (1, 0) -> (1, 1) We are returning here +100

(0, 0) -> (1, 1) -> (0, 1) Here we return +0

If you look at this, the algorithm should actually play the 3rd branch and go immediately to (1, 1), but instead select the 1st or 2nd branch. And after playing this shot, he will find himself in the same situation and will continue to select the branches that have (1, 1) as 2nd step.

How to solve this problem? My game has entered a sort of infinite situation where it only plays another shot in the hope of playing a good deep shot 1, but it repeats the same process and repeats it to infinity.

encryption – Is this key exchange algorithm secure and valid?

I write a reliable UDP implementation and I want it to be secure. In addition, I want to use elliptic curve cryptography. I do not have the training needed to fully understand the math behind this type of cryptography, but I understand how to use it (at least in general).

Given the next prerequisites, is this algorithm secure? What steps can be ignored without compromising security?

(opt.) Is it acceptable for the parties to use a single ECDSA key pair for all interactions over a long period? I mean, do not change that key pair and use it to sign messages.


  • The interaction happens between two parties A and B
  • Both A and B have predefined ECDSA key pair
  • The digital signature also works as a hash


  1. A calculate and send to B her epheremal ECDH Public key
  2. B calculate the sharing epheremal ECDH secret and ax with SHA-256using the key A sent it to step 1
  3. B randomly generates 256 bits static Encryption key
  4. B the crypt ECDSA public key and the static step 3 encryption key with the help of the epheremal secret of step 2 with AES-256 algorithm
  5. B sign the result of step 4 with it ECDSA private key
  6. B send to A the result of step 4 next to the signature of step 5 and its epheremal ECDH Public key
  7. A also calculates the sharing epheremal ECDH secret and ax with SHA-256using the key B sent it to step 6
  8. A decrypts the result of step 4 using epheremal key of step 7 with AES-256 algorithm
  9. A check the signature B sent him at step 6 using Bof ECDSA public key that she received at step 8
    • If something goes wrong (the signature is invalid or step 8 leads to corrupted data), A abandoned
    • That much, A has everything she needs to send messages safely
  10. A the sign and the number ECDSA public key using AES-256 algorithm with the static encryption key received at step 8
    • A can also sign and encrypt useful data at this stage
  11. A send to B the result of step 11 with the signature
  12. B decrypts the ciphertext A sent him using AES-256 algorithm and verifies the signature
    • If here something is wrong, B abandoned
  13. Now both A and B know ECDSA public keys from each other and the sharing encryption key, so that they can interact securely and make sure that no one can change their messages

discrete mathematics – Demonstrate that x and y in the extended Euclidean algorithm will not overflow an integer (Si

We are given a and b <= 1e8.

The extended Euclid algorithm always finds a solution for ax + by = gcd (a, b) (assuming it exists) that can always be stored in an Int.

How to prove that x and y do not overflow an int?

code: https://cp-algorithms.com/algebra/extended-euclid-algorithm.html

array – An algorithm that associates consecutive integers with sets of numbers

Where would I start if I wanted to develop an algorithm that maps consecutive integers to a long list of unique sets of integers. For example:

0 = {0, 0, 0}  
1 = {0, 0, 1}  
2 = {0, 0, 2}  
3 = {0, 0, 3}
4 = {0, 0, 4}
5 = {0, 0, 5}
6 = {0, 0, 6}
7 = {0, 0, 7}  
8 = {0, 0, 8}  
9 = {0, 0, 9}  
29436 = {19, 43, 11}
29437 = {19, 43, 12} 
29438 = {19, 43, 13}
29439 = {19, 43, 14} 
29440 = {19, 43, 15}
29441 = {19, 43, 16} 
29442 = {19, 43, 17}
29443 = {19, 44, 0}  
29444 = {19, 44, 1}  
29445 = {19, 44, 2}  
29446 = {19, 44, 3}  
64362 = {78, 5, 0} 
64363 = {78, 5, 1} 
64364 = {78, 5, 2} 
64365 = {78, 5, 3} 
64366 = {78, 5, 4} 
64367 = {78, 5, 5} 
64368 = {78, 5, 6} 
64369 = {78, 5, 7}

When an integer (number to the left of the sign =) is entered, the algorithm must return the set of numbers assigned to the integer. The set of numbers must be considered completely random. The list of number sets will be quite long but the exact number of sets of numbers is known. Is it possible? Is machine learning necessary for this? Is there already an algorithm that can explain these relationships?

data structures – nested box algorithm

I am given a set of 3D boxes {$ B_1 $, $ B_2 $$ B_n $} and each box has a length, a width, and a height. But these values ​​are interchangeable since I can rotate the box. I need to know the maximum sequence of nested boxes.

I've tried this problem using spell. But I'm still stuck. I've used the built-in sort function to sort all boxes by length in ascending order. I read the reference to this question and learned that people were using basic sorting. I have an idea of ​​how the basic type is applied here because all the types I know are types of comparison.

Algorithm to minimize a regular expression

I'm talking about regular expressions with alphabet {$ 0, $ 1 $}. We want to minimize them so that they have the least possible symbols and operators. Is there an algorithm to do it?

For example, what is done on this page in the accepted answer:


Is there a formal algorithm to explain the process followed by the answer?

Algorithm for calculating $ Pr[s vDash C bigcup^{geq n} B]$ for probabilistic verification

I have trouble finding a computer algorithm $ Pr [s vDash C ~ bigcup ^ { geq n} B] $ given a finite Markov chain where $ S $ is the set of states, $ s in S $, $ B, C subseteq S $, and $ n in mathbb {N} $ or $ n geq 1 $.

I have algorithms for computer science $ Pr [s vDash C ~ bigcup ~ B] $, $ Pr [s vDash C ~ bigcup ^ { leq n} ~ B] $, and $ Pr [s vDash C ~ bigcup ^ {= n} ~ B] $. Instead of going down to the Markov chain itself, I was thinking of using a combination of these algorithms to compute $ Pr [s vDash C ~ bigcup ^ { geq n} B] $However, I am afraid of counting too much because it is not necessarily true that $ B $ and $ C $ are disjointed.

I use the book Principles of Model Checking by Baier and Katoen.

python – Optimization of a raycasting / voxelspace algorithm in Pygame

These last three days, I have developed a simple ray scattering algorithm. I've made the rendering engine work as I would like. However, it is extremely slow (5 frames per second) because of two things (as far as I know).

  1. Find the values ​​of a pixel independently several times, and often it falls on the same pixel.
  2. The large amount of times the code is executed.

I am aiming for speed, but the only programming language in which I have experience is Python. What optimizations can I do for this code? What other libraries can I use that are faster than Pygame or PIL for this purpose?
Also, how to create the coordinate loop in pixels (-1 = 1023) without the if statements to prevent the game from crashing so close to the boundary?

from PIL import Image
import pygame

cdef enum resolution:

cdef int uu 
cdef int height 
cdef int prevHeight 
cdef float t 
cdef float foga
cdef int fogb
cdef int fogc

cdef int x
cdef int y
cdef extern from "math.h":
    double sin(double x)
    double cos(double x)

rgb_im = col.convert('RGB')

screen = pygame.display.set_mode((resX,resY))

def Render(pX,pY,pAngle):
  for v in range(-qresX,qresX):
    for u in range(1,125):
      if u<30:
      elif u<60:
      if height>prevHeight:
        pygame.draw.line(screen, (r/foga+fogb,g/foga+fogb,b/foga+u), (qresX+v,resY-prevHeight),(qresX+v,resY-height),1)

I'm compiling this code as .so (via Cython) and launch it from another .py. Is it faster if I just use PyPy instead?