Find pairs that sum to X in an array of integers of size N having element in the range 0 to N1
By : Billy Bob
Date : March 29 2020, 07:55 AM
I wish this helpful for you No, I don't believe so. You either need extra space to be able to "sort" the data in O(n) by assigning to buckets, or you need to sort inplace which will not be O(n). code :
0 1 2 3 4 5 6 7 < index
(0)7 (0)6 (0)2 (0)5 (0)3 (0)3 (0)7 (0)7
for idx = 0 to N:
array[array[idx] % 16] += 16 // add 1 to top four bits
0 1 2 3 4 5 6 7 < index
(0)7 (0)6 (1)2 (2)5 (0)3 (1)3 (1)7 (3)7
0 1 2 3 4 5 6 7 < index
(0) (0) (1) (2) (0) (1) (1) (3)
a b c d e f g h < identifiers
7 6 2 5 3 3 7 7
(2,6) (3,5) (3,5)
(c,b) (e,d) (f,d) < identifiers
#include <stdio.h>
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 4, 4, 4, 4};
#define SZ (sizeof(arr) / sizeof(*arr))
static void dumpArr (char *desc) {
int i;
printf ("%s:\n Indexes:", desc);
for (i = 0; i < SZ; i++) printf (" %2d", i);
printf ("\n Counts :");
for (i = 0; i < SZ; i++) printf (" %2d", arr[i] / 100);
printf ("\n Values :");
for (i = 0; i < SZ; i++) printf (" %2d", arr[i] % 100);
puts ("\n=====\n");
}
int main (void) {
int i, j, find, prod;
dumpArr ("Initial");
// Sort array in O(1)  bucket sort.
for (i = 0; i < SZ; i++) {
arr[arr[i] % 100] += 100;
}
dumpArr ("After bucket sort");
// Now do pairings.
find = 8;
for (i = 0, j = find  i; i <= j; i++, j) {
if (i == j) {
prod = (arr[i]/100) * (arr[i]/1001) / 2;
if (prod > 0) {
printf ("(%d,%d) %d time(s)\n", i, j, prod);
}
} else {
if ((j >= 0) && (j < SZ)) {
prod = (arr[i]/100) * (arr[j]/100);
if (prod > 0) {
printf ("(%d,%d) %d time(s)\n", i, j, prod);
}
}
}
}
return 0;
}
Initial:
Indexes: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Counts : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Values : 3 1 4 1 5 9 2 6 5 3 5 8 9 4 4 4 4
=====
After bucket sort:
Indexes: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Counts : 0 2 1 2 5 3 1 0 1 2 0 0 0 0 0 0 0
Values : 3 1 4 1 5 9 2 6 5 3 5 8 9 4 4 4 4
=====
(2,6) 1 time(s)
(3,5) 6 time(s)
(4,4) 10 time(s)

Want to find value which lies within range of array fields
By : Ashveen
Date : March 29 2020, 07:55 AM
like below fixes the issue I think you are misusing the find function. It's far more powerful than you think :p Actually, find takes only one argument, which is a callback which will be called with three arguments : the element to test, the index of that element, and the array. You will return true if the tested element is valid. documentation here : https://developer.mozilla.org/enUS/docs/Web/JavaScript/Reference/Global_Objects/Array/find code :
var myElement = User.find(function(element, index, array){
return element["_id"] == req.params._id && element.reciptBook.startingReciptNumber == {$gte: +req.params.reciptValue};
})

How could I find patterns of repeating pairs of integers in an array or list of integers?
By : Timothy de Vos
Date : March 29 2020, 07:55 AM
hope this fix your issue Assuming you are looking for B to contain a list of all patterns in A that begin on integer x (default at 2), end on integer y (default at 1), and alternate between the values x and y, then here's how you can generate B: code :
def special_pattern(A, x=2, y=1):
B = []
temp = []
flag = 0
for elem in A:
if elem == x and flag == 0:
flag = 1
elif elem == y and flag == 1:
temp.extend([x, y])
flag = 0
else:
if temp != []:
B.append(temp)
temp = []
if not(elem == x and flag == 1):
flag = 0
if temp != []:
B.append(temp)
return B

I am using a numpy array of randomly generated ordered pairs, I need to determin if the ordered pairs are different type
By : Tolga Bozkurt
Date : March 29 2020, 07:55 AM
hop of those help? You've described well how you are storing the data, but not what the algorithm is. For example, if we want to answer the question "Is this set of three (x,y) points P1..P3 an equilateral triangle," we can formulate it this way: code :
dist(P1,P2) == dist(P2,P3) == dist(P3,P1)
sqrt((P1.x  P2.x)**2 + (P1.y  P2.y)**2)
P1.x, P1.y, P2.x, P2.y, P3.x, P3.y
A[0]  A[2] # P1.x  P2.x
A[1]  A[3] # P1.y  P2.y
A[2]  A[4]
A[3]  A[5]
A[4]  A[0]
A[5]  A[1]
R = A  np.roll(A, 2, axis=0) # 1000x6 array of all differences
R[0] + R[1] # (P1.x  P2.x)**2 + (P1.y  P2.y)**2
R[2] + R[3]
R[4] + R[5]
S = R[0::2] + R[1::2] # three columnwise additions at once
np.logical_and(S[0] == S[1], S[1] == S[2])
A = pnts.T
R = np.square(A  np.roll(A, 2, axis=0))
S = R[0::2] + R[1::2] # 1000x3 squares of distances
np.logical_and(S[0] == S[1], S[1] == S[2]) # 1000 True/False results

How to transform in Java an array of ordered pairs in a 2D array of integers
By : user3122777
Date : March 29 2020, 07:55 AM

