Efficient algorithm for finding the optimal sum of both arrays

I have two arrays (A, B) and a sum M. I have to find a number from each array that yields the sum M. If there are no two elements making up the sum M, the nearest and less than the sum M is the solution. See examples below. What is the efficient way to find the solution?

eg1:
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.

eg1:
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.

Posted on Categories Articles

c ++ sorting algorithm First k-element

It gives me the wrong issue. what is the problem.
I want to sort the numbers exactly, how can I do that?
How can I move the array and insert the numbers in the correct positions?

'''
int a;
int N = 0;

cin >> N;  // Taking size of the inputs
int* numbers = new int(k);  // ı want the kth greatest number .
int x = 0;
while (x < k) {
cin >> a;
numbers(x) = a;
x++;
}

int i, anahtar, j;  // after ı take k number from user, I Sort the numbers via insertion sort
for (i = 1; i < k; i++)
{
anahtar = numbers(i);
j = i - 1;

while (j >= 0 && numbers(j) > anahtar)
{
numbers(j + 1) = numbers(j);
j = j - 1;
}
numbers(j + 1) = anahtar;
}

// then I am taking other numbers one by one and ı am comparing with numbers in the array. After ı insert it the correct place, shifthing the array to the right

for (int e = x; e < N; e++) {
cin >> a;
if (a > numbers(k-1)) {
for (int i = k-2; i >= 0; i--) {
if (i == 0) {
numbers(i+1) = numbers(i);
numbers(i) = a;
}

else if (a > numbers(i)) {
numbers(i+1) = numbers(i);
}
else {
numbers(i+1) = a;
}
}

}

}

return numbers(k -1);

'''

Algorithm – I do not have any of the best programming languages ​​for games. Is that really helpful enough to have a good capacity for games?

Yes, since C ++ is a high-level language that teaches you the basics of object-oriented programming, it is a good idea to learn it. It's also the language used to build most major console and Windows games. … C ++ is not an easy language to learn.

Posted on Categories Articles

How can I avoid recalculating the Dijkstra algorithm when adding or removing an edge from a graphic?

Thank you for sending a reply to Computer Science Stack Exchange!

• Please be sure too answer the question, Provide details and share your research!

But avoid

• Ask for help, clarify or respond to other answers.
• Make statements based on opinions; Cover them with references or personal experience.

Use MathJax to format equations. Mathjax reference.

For more information, see our tips for writing great answers.

Posted on Categories Articles

matrix – Algorithm that converts array-like data to MathJax: Python

I often use MathJax and often have to write matrices. Since writing matrices with MathJax is very tedious, I opted for automation.

Code:

import numpy as np
import pyperclip

class Conversion():

def __init__(self, matrix, n = None, m = None):
if matrix == None:
self.matrix = np.random.randint(-10,10, size = (n,m))
else:
'''If variable "matrix", for example, contains (1,2,3,4), instead of ((1,2,3,4)), functions below
will throw error, hence we need to make sure that the data type is correct.'''
if type(matrix(0)) in (str,int,float):
self.matrix = np.array((matrix,))
else:
self.matrix = np.array(matrix)

self.rows = len(self.matrix)
self.cols = len(self.matrix(0))

def single(self, choosen_brackets = ')'):
available_brackets = {' ' : 'matrix',
')' : 'pmatrix',
')' : 'bmatrix',
'}' : 'Bmatrix',
'|' : 'vmatrix',
'||': 'Vmatrix'  }
choosen_brackets = available_brackets(choosen_brackets)
body = '$$\begin {} n'.format('{' + choosen_brackets + '}') for row in self.matrix: row = (str(x) for x in row) body += ' & '.join(row) + '\\' + 'n' body +='end {}$$'.format('{' + choosen_brackets + '}')
print(body)
pyperclip.copy(body)

def augmented(self, choosen_brackets = '()'):
'''We are assuming that the last column of the given matrix is a vector.'''

pos_of_the_verticar_bar = '{' + 'c'*(self.cols-1) + '|' + 'c' + '}'
body = '$$\left ( \begin {array} %s n' % (pos_of_the_verticar_bar) for row in self.matrix: row = (str(x) for x in row) body += ' & '.join(row) + '\\' + 'n' body +='end {array} \right )$$'
print(body)
pyperclip.copy(body)

Remarks:

1. There work augmented is pretty much too singleI could have merged them into one. However, I think that the separation of the codes improves readability.

2. MSE includes Mathjax equations  Instead of \$

3. "single"is a bad name for a function, I admit, I have not found any better options, do not hesitate to offer suggestions.

What can be improved?

Posted on Categories Articles

discrete mathematics – Prove that x and y in the extended Euclid algorithm do not overflow an integer (If a, b

We get a and b <= 1e8.

The extended Euclid algorithm always finds a solution for ax + by = gcd (a, b) (if any), which can always be stored in an int.

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

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

Posted on Categories Articles

array – Algorithm that maps consecutive integers to 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}

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

Posted on Categories Articles

Caching – Map synchronization algorithm

Suppose I have two Map Instances in JavaScript (or HashMap in Java or Dictionary in C #), A and B. I want to sync B to A, so B is a copy of A. I can not copy the reference and do not want to delete and re-add all keys. I want B to be available and mostly accurate during processing. What is the most efficient algorithm for this?

I am looking for a general algorithm for cache synchronization while the second cache remains available. This seems to be the same as dealing with cards.

Posted on Categories Articles

Data Structures – Algorithm for nested boxes

I get a bunch of 3d boxes {$$B_1$$. $$B_2$$$$B_n$$} and each box has length, width and height. But these values ​​are interchangeable since I can turn the box. I have to find out the maximum order of nested boxes.

I have tried this problem with sort. But I'm still stuck. I used the built-in sort function to sort all boxes in ascending order by length. I read the reference to this question and learned that people were using radix sorting. I have an idea of ​​how the radix sorting is applied here, since all sorts known to me are the comparison sort.

Posted on Categories Articles

Dynamic Programming – Search for an algorithm for finding the partition of data at an interval that maximizes the minimum fitness between the blocks

In the work "An algorithm for optimal partitioning of data in an interval" (link), the authors describe an algorithm for partitioning data in an interval in order to maximize a fitness function. The fitness function they use is the sum of the fitness of each block in the partition.

My question is, how can this be done if the fitness function were the minimum fitness of each block instead? More specifically, what should the algorithm be like if the sum in the fitness function in equation (4) has been replaced by min?

I suspect that the same algorithm would work, replacing Equation (6) with the following:

$$Max_j , {min (opt (j-1), end (j, n + 1)) }.$$

I would think that this has been done before, but I can not find any references. Does anyone have a reference or does he agree that my change is correct?

Posted on Categories Articles