Given some integers, find triplets where the product of two numbers equals the third number
up vote
0
down vote
favorite
I am trying to solve a problem from coding challenge and the question is described as follows
Given N integers A1, A2, ..., AN, count the number of triplets (x, y,
z) (with 1 ≤ x < y < z ≤ N) such that at least one of the following is
true:
Ax = Ay × Az, and/or
Ay = Ax × Az, and/or
Az = Ax × Ay
5 2 4 6 3 1
In Sample Case #1, the only triplet satisfying the condition given in the problem statement is (2, 4, 5). The triplet is valid since the second, fourth, and fifth integers are 2, 6, and 3, and 2 × 3 = 6. thus the answer here is 1
2 4 8 16 32 64
the six triplets satisfying the condition given in the problem statement are: (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), (2, 3, 5), (2, 4, 6). so the answer here is 6
My Code in python:
import itertools
count=0
for t in itertools.combinations(l,3):
if t[0]*t[1]==t[2] or t[1]*t[2]==t[0] or t[0]*t[2]==t[1]:
count+=1
print(count)
So this is the naive way of generating all possible 3 length combinations and checking for the condition. This works fine for smaller input but when the inout size increase time complexity increases. I am assuming for an example that has 1,2,3,6,8
the combinations generated are (2,3,6),(2,3,8)
2,3,6 satisfy the condition so the checking for 2,3,8 is unnecessary and can be avoided. How can I modify my code to take advantage of this observation ?
python performance algorithm python-3.x programming-challenge
add a comment |
up vote
0
down vote
favorite
I am trying to solve a problem from coding challenge and the question is described as follows
Given N integers A1, A2, ..., AN, count the number of triplets (x, y,
z) (with 1 ≤ x < y < z ≤ N) such that at least one of the following is
true:
Ax = Ay × Az, and/or
Ay = Ax × Az, and/or
Az = Ax × Ay
5 2 4 6 3 1
In Sample Case #1, the only triplet satisfying the condition given in the problem statement is (2, 4, 5). The triplet is valid since the second, fourth, and fifth integers are 2, 6, and 3, and 2 × 3 = 6. thus the answer here is 1
2 4 8 16 32 64
the six triplets satisfying the condition given in the problem statement are: (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), (2, 3, 5), (2, 4, 6). so the answer here is 6
My Code in python:
import itertools
count=0
for t in itertools.combinations(l,3):
if t[0]*t[1]==t[2] or t[1]*t[2]==t[0] or t[0]*t[2]==t[1]:
count+=1
print(count)
So this is the naive way of generating all possible 3 length combinations and checking for the condition. This works fine for smaller input but when the inout size increase time complexity increases. I am assuming for an example that has 1,2,3,6,8
the combinations generated are (2,3,6),(2,3,8)
2,3,6 satisfy the condition so the checking for 2,3,8 is unnecessary and can be avoided. How can I modify my code to take advantage of this observation ?
python performance algorithm python-3.x programming-challenge
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to solve a problem from coding challenge and the question is described as follows
Given N integers A1, A2, ..., AN, count the number of triplets (x, y,
z) (with 1 ≤ x < y < z ≤ N) such that at least one of the following is
true:
Ax = Ay × Az, and/or
Ay = Ax × Az, and/or
Az = Ax × Ay
5 2 4 6 3 1
In Sample Case #1, the only triplet satisfying the condition given in the problem statement is (2, 4, 5). The triplet is valid since the second, fourth, and fifth integers are 2, 6, and 3, and 2 × 3 = 6. thus the answer here is 1
2 4 8 16 32 64
the six triplets satisfying the condition given in the problem statement are: (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), (2, 3, 5), (2, 4, 6). so the answer here is 6
My Code in python:
import itertools
count=0
for t in itertools.combinations(l,3):
if t[0]*t[1]==t[2] or t[1]*t[2]==t[0] or t[0]*t[2]==t[1]:
count+=1
print(count)
So this is the naive way of generating all possible 3 length combinations and checking for the condition. This works fine for smaller input but when the inout size increase time complexity increases. I am assuming for an example that has 1,2,3,6,8
the combinations generated are (2,3,6),(2,3,8)
2,3,6 satisfy the condition so the checking for 2,3,8 is unnecessary and can be avoided. How can I modify my code to take advantage of this observation ?
python performance algorithm python-3.x programming-challenge
I am trying to solve a problem from coding challenge and the question is described as follows
Given N integers A1, A2, ..., AN, count the number of triplets (x, y,
z) (with 1 ≤ x < y < z ≤ N) such that at least one of the following is
true:
Ax = Ay × Az, and/or
Ay = Ax × Az, and/or
Az = Ax × Ay
5 2 4 6 3 1
In Sample Case #1, the only triplet satisfying the condition given in the problem statement is (2, 4, 5). The triplet is valid since the second, fourth, and fifth integers are 2, 6, and 3, and 2 × 3 = 6. thus the answer here is 1
2 4 8 16 32 64
the six triplets satisfying the condition given in the problem statement are: (1, 2, 3), (1, 3, 4), (1, 4, 5), (1, 5, 6), (2, 3, 5), (2, 4, 6). so the answer here is 6
My Code in python:
import itertools
count=0
for t in itertools.combinations(l,3):
if t[0]*t[1]==t[2] or t[1]*t[2]==t[0] or t[0]*t[2]==t[1]:
count+=1
print(count)
So this is the naive way of generating all possible 3 length combinations and checking for the condition. This works fine for smaller input but when the inout size increase time complexity increases. I am assuming for an example that has 1,2,3,6,8
the combinations generated are (2,3,6),(2,3,8)
2,3,6 satisfy the condition so the checking for 2,3,8 is unnecessary and can be avoided. How can I modify my code to take advantage of this observation ?
python performance algorithm python-3.x programming-challenge
python performance algorithm python-3.x programming-challenge
edited 2 mins ago
200_success
127k15148412
127k15148412
asked 1 hour ago
Rohit
5333820
5333820
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208662%2fgiven-some-integers-find-triplets-where-the-product-of-two-numbers-equals-the-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown