## python – K Inverse Pairs Array – Brute Force

For an integer array nums, an inverse pair is a pair of integers (i, j) where 0 <= i < j < nums.length and nums(i) > nums(j).

Given two integers n and k, return the number of different arrays consist of numbers from 1 to n such that there are exactly k inverse pairs. Since the answer can be huge, return it modulo 10^9 + 7.

https://leetcode.com/problems/k-inverse-pairs-array/solution/

Brute Force Solution:

``````from itertools import permutations, combinations

class Solution:
def kInversePairs(self, n: int, k: int) -> int:
array = (x for x in range(1,n+1))
array_permutations = list(permutations(array))
memoized = {}
count = 0
for each_permutation in array_permutations:
each_permutation = list(each_permutation)
if self.has_exactly_k_inverse_pair(k, each_permutation, memoized):
count += 1
return count % 1000000007

def has_exactly_k_inverse_pair(self, k, array, memoized):
count = 0
list_of_sequential_pairs = combinations(array, 2)
for item_i, item_j in list_of_sequential_pairs:
cached = memoized.get((item_i, item_j), None)
if not cached:
result = self.is_inverse_pair((item_i, item_j))
if result:
memoized((item_i, item_j)) = result
count += 1
else:
count += 1

if count > k:
return False
return count == k

def is_inverse_pair(self, tup):
return tup(0) > tup(1)

``````

I am looking for code review just for the brute force solutions. I am looking too get constructive feedback mainly with:

• Using `memoized` dictionary for storing `True` entries only.
• Being able to pass memoized and update it
• Using tuples as keys in python
• Overall best coding practices.

## private key – Is it possible to find additional key pairs resulting in the same address that I already control?

I’m not really well versed in the necessary terminology to get the answer I need from searching.
As I understand, there are 2^160 possible addresses, and 2^256 possible private keys, so each address corresponds to roughly 2^(256-160)=2^96 private keys.

Let’s say I generate a key pair and the corresponding address, how would one go about finding other key pairs that correspond to the same address if you already have at least 1 set of keys that generated that address? Is it possible?

Apologies if this has been answered already, I couldn’t find the answer I am looking for.

Posted on Categories Articles

## algorithms – Number of combinations without given pairs

Given set of elements {e1, e2, … en}, set of pairs of this elements (each element may be present in several pairs) and a number k.

I need count how many combinations of size k exists which not include any pair?

For example: if there only one pair {e1, e2} result in number of all combinations without elements e1 and e2. It may be counted in constant time: $$C^k_n – C^{k-2}_{n-2}$$

This task can be solved with naive algorithm on python:

``````def combinations_without_pairs(elements, pairs, k):
def iterable_len(iterable):
return sum(1 for _ in iterable)

def not_include_pairs(combination):
for pair in pairs:
if pair(0) in combination and pair(1) in combination:
return False
return True

return iterable_len(filter(not_include_pairs, itertools.combinations(elements, k)))
``````

But this algorithm is ineffective: it requires to iterate over each element combination and each pair.

Is there any way to solve this problem with better complexity? At least reduce impact of big count of elements and pairs?

I want to find algorithm with lowest time complexity.

Posted on Categories Articles

## c++ – Finding all pairs of elements in an array that sum to a target value using multiple pointers (follow up)

The previous question was not clear enough so I will try to clarify it more in this follow-up question.
I tried the multiple pointers technique to solve the problem that find pair of values that sums up to a target value.

We are assuming that:

• the array is already sorted.
• we are working with a static array of size 16 (testing purpose only).
• we are not working with actual pointers; because we don’t need them in this case (I think), I named the question on the algorithm’s
name only.
``````    #include <iostream>
#include <vector>
using namespace std;

struct result
{
int num1;
int num2;
result() {}

result(int num1, int num2)
: num1{ num1 }, num2{ num2 } {}
};

vector<result> mult_pt(int arr(), int target)
{
int p1 = 0;
int p2 = 15;
bool flag = false;
vector<result> res;
while (p1 <= p2)
{
if (arr(p1) + arr(p2) == target)
{

for (int k = 0; k < res.size() - 1; k++)
{
if (res.size() == 0)
res.push_back(result(arr(p1), arr(p2)));
else if (res(k).num1 != arr(p1) && res(k).num2 != arr(p2))
{
flag = false;
}
else flag = true;
}
if(flag) res.push_back(result(arr(p1), arr(p2)));
p1++;
}
else if (arr(p1) + arr(p2) < target)
{
p1++;
continue;
}
else if (arr(p1) + arr(p2) > target)
{
p2--;
for (int i = p1; i >=0; i--)
{
if (arr(i) + arr(p2) == target)
{
res.push_back(result(arr(p1), arr(p2)));
}
else if (arr(i) + arr(p2) > target)
{
continue;
}
else break;
}
}
}
return res;
}

int main ()
{
int array(16) = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
vector <result> res = mult_pt(array, 19);
for (int j = 0; j < res.size(); j++)
{
cout << "( " << res(j).num1 << " , " << res(j).num2 << " )" << endl;
}
return 0;
}

``````

The output should be like:

``````( 4 , 15 )
( 5 , 14 )
( 6 , 13 )
( 7 , 12 )
( 8 , 11 )
( 9 , 10 )
( 10 , 9 )
``````

You can check the previous post here.

Posted on Categories Articles

## c++ – Finding all pairs of elements in an array that sum to a target value using multiple pointers

I was thinking about this problem where you should make a function that takes an array and a target value and then it will find all the pair values that sums up to that target value.

Of course there is the naive way which includes nested loops, but I want to avoid the O(n^2) time complexity so I have came up with this solution.

I want to know:

– is it more efficient & how to calculate it’s Big O?

– what are the other possible solutions that can solve the same
problem?

``````void pairs_sum_up_to_value(int arr(), int target)
{
int p1 = 0;
int p2 = 15;
int flag = 2;
struct result
{
int num1;
int num2;
result() {}

result(int num1, int num2)
: num1{ num1 }, num2{ num2 } {}
};
vector<result> res;
while (p1 < p2)
{
if (arr(p1) + arr(p2) == target)
{

for (int k = 0; k < res.size() - 1; k++)
{
if (res.size() == 0)
res.push_back(result(arr(p1), arr(p2)));
else if (res(k).num1 != arr(p1) && res(k).num2 != arr(p2))
{
flag = 1;
}
else flag = 0;
}
if(flag == 0) res.push_back(result(arr(p1), arr(p2)));
p1++;
}
else if (arr(p1) + arr(p2) < target)
{
p1++;
continue;
}
else if (arr(p1) + arr(p2) > target)
{
p2--;
for (int i = p1; i >=0; i--)
{
if (arr(i) + arr(p2) == target)
{
res.push_back(result(arr(p1), arr(p2)));
}
else if (arr(i) + arr(p2) > target)
{
continue;
}
else break;
}
}
}
``````

Posted on Categories Articles

## machine learning – Separating objects into a constrained number of groups where data is known about all object pairs

In Frequency Planning, we may know the interference between one cell and another cell. You may be a able to operate a specific number of frequencies. How would one separate them into the best groups. I have been toying with Classify but am unable to see how to program it to perform the required grouping.

I have a program to produce sample data.

``````Combinations(all___) := Flatten(Outer(List, all, 1), Length({all}) - 1);
all=ToString/@Range(6);
groups=ToString(#)&/@Range(2);
contents=Association();
(contents(#)=groups((1)))&/@all((;;3));
(contents(#)=groups((2)))&/@all((4;;));
contents;
reverseContents=contents//Normal//GroupBy(Last->First);
makeValues({a_,b_}):=Module({},
If(a==b,Missing(),
c=contents(a)==contents(b);
If(c,
{a,b,RandomReal({-28,-50}),{a,b}->"same group"},
{a,b,RandomReal({0,-30}),{a,b}->"different group"}
)));
training=Select(makeValues/@Combinations(all,all),Not(MissingQ(#))&);
``````

The data as I have programmed gives the following dataset where the fourth column is the recommendation. Either the nodes should be in the same group or they should be in different groups.

``````Grid(training)
1   2   -36.0319    {1,2}->same group
1   3   -33.4696    {1,3}->same group
1   4   -26.8633    {1,4}->different group
1   5   -18.9969    {1,5}->different group
1   6   -14.5339    {1,6}->different group
2   1   -42.4544    {2,1}->same group
2   3   -32.0968    {2,3}->same group
2   4   -25.3401    {2,4}->different group
2   5   -19.9827    {2,5}->different group
2   6   -7.73741    {2,6}->different group
3   1   -49.5421    {3,1}->same group
3   2   -41.9788    {3,2}->same group
3   4   -13.1989    {3,4}->different group
3   5   -18.0183    {3,5}->different group
3   6   -10.5025    {3,6}->different group
4   1   -17.6852    {4,1}->different group
4   2   -0.997097   {4,2}->different group
4   3   -5.0447     {4,3}->different group
4   5   -47.2881    {4,5}->same group
4   6   -43.6139    {4,6}->same group
5   1   -20.8662    {5,1}->different group
5   2   -29.4012    {5,2}->different group
5   3   -4.96949    {5,3}->different group
5   4   -31.5497    {5,4}->same group
5   6   -29.7002    {5,6}->same group
6   1   -3.60692    {6,1}->different group
6   2   -28.6723    {6,2}->different group
6   3   -12.8056    {6,3}->different group
6   4   -44.3089    {6,4}->same group
6   5   -48.3195    {6,5}->same group
``````

This is a simple example I plan to expand to hundreds of cells and up to 10 groups. Anybody’s help would be much appreciated.

Posted on Categories Articles

## co.combinatorics – Number of distinct pairs which doesn’t share difference

Inspired by This question by Vidyarthi I tried to find the value of $$T(2m)$$ where, $$T(2m)$$ is the number of sets of distinct pairings (so, the sets have $$m$$ elements) of the numbers $$1,2,3….,2m$$ such that for no two pairs of a set $$(a,b)$$ and $$(c,d)$$ $$|a-b|=|c-d|$$. But I couldn’t find any reasonable way to solve it. Any answers and comments are welcome.

$$T(2m)$$ is odd. Because, for a set $$(a_1,a_2);(a_3,a_4);….;(a_{2m-1},a_{2m})$$ we can swap $$a_i$$ by $$2m+1-a_i$$ for all $$i$$. So there is a dual set for each set of pairs except for $${(1,2m);(2,2m-1);…;(m-1,m+1)}$$ whose swapped-pair is this itself.

Using $$T(2m)$$ we may show directly the referenced question: Let, we have a set of differences (in modulo $$k+1$$) which has no such solution pairs (as asked in the referenced question) be $$l_1,l_2…,l_m$$ then in this question all sets of differences $$(l_1,k+1-l_1);(l_2,k+1-l_2);…;(l_m,k+1-l_m)$$ are prohibited (taking one from each bracket/ due to the modulo argument). Hence, total $$2^m$$ are prevented. If, we can show that $$T(2m)>binom{2m-1}{m}-2^m=frac{1}{2}binom{2m}{m}-2^m$$ then, the referenced question is proved.

e.g for $$2m=6$$, $$T(6)=5$$ and $$binom{6-1}{3}-2^3=2$$. Also,for $$2m=8, T(8)>binom{7}{4}-2^4=19$$

Posted on Categories Articles

## mysql – Group by unique pairs with no multiplicity

I have a table AreaLocation with AreaID as the PKey and Location whereby there are many locations for each AreaID. Correspondingly there is another table Books with:

``````BookingID | DriverID | BLocation | Miles | Fare
``````

I’m trying to get the average efficiency of each driver by area, i.e. I’d looking for a view with

``````| DriverID | AreaID | AVERAGE(Fare/Miles) |
``````

where I’d like to group by distinct DriverID/AreaID however I’m not sure whether the following will work:

``````CREATE VIEW EFFICIENCY AS
SELECT AreaID, Bookings.DriverID, AVERAGE(Bookings.Fare/Bookings.Miles)
FROM AreaLocations
RIGHT JOIN Bookings ON
( AreaLocations.Location = Bookings.Location )
GROUP BY DISTINCT (AreaID, Bookings.DriverID)
``````

as I don’t think distinct works on pairs.

Posted on Categories Articles

## algorithms – Finding all pairs of points with no point in between

Suppose there are $$n$$ points $$p_1,p_2,dots,p_n$$ with color red or blue on a line. We want to find all pairs $$(p_i,p_j)$$ whose color is distinct and such that there are no points between them. If there are $$k$$ pairs with the described property, design an algorithm with $$O(nlog k)$$ that uses idea of divide and pruning.

I think if we check all points we can solve this problem, but running time will exceed $$O(nlog k)$$.

I think for solving this problem we can use projective geometry duality, but I am stuck. Any help would be appreciated.

Posted on Categories Articles

## algorithms – Finding distinct pairs in geometric settings

Suppose there are $$n$$ points $$p_1,p_2,…,p_n$$ with color red or blue on a line. We want to find all pairs $$(p_i,p_j)$$ their color is distinct and there are no points between theme. If there are $$k$$ pairs with described design an algorithm with $$O(nlog k)$$ that use idea divide and pruning.

I think if we check all point we can solve this problem , but running time is not $$O(nlog k)$$.

I think for solving this problem we can use the duality that present in this page

But i get stuck to solve my problem. any help be appreciated.

Posted on Categories Articles