We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Suppose you have triples x,y,z where the relationship is y = x*r, z = y *r. The trick is that when you first see a number, you consider it x and you know what will be the value of next value x*r, you save it in a dict or map v2. In the next iterations, you will search for the expected number y (x*r), if you find it, similar way you save the next value z = y *r in the v3. So, anything in v3, you know it is your second element. next time, when you see the value you z, you will get your expected triple... it is the way it works
suppose 1,3,9,9,27,81 example. In the first iteration there is no value for count and v3, because right side v3[1] and v2[1] doesn't exists, but add 1 to the v2[3] .
In the second iteration. you don't have any value for count as v3[3] doesn't exist. But you have v2[3] from previous iteration. So, now v3[9] = 1 and v2[9] = 1. so, the value v3[3] means you got 2 value for your triple.
In the third iteration, count is 1 as you v3[3] has value from previous iteration and the triple are completed. Similiarly next moves are continued
if you still don't understand, just print(k, v3, v2) at the end of your for loop, you will see the changes of each iteration and make sense.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Count Triplets
You are viewing a single comment's thread. Return to all comments →
Suppose you have triples x,y,z where the relationship is y = x*r, z = y *r. The trick is that when you first see a number, you consider it x and you know what will be the value of next value x*r, you save it in a dict or map v2. In the next iterations, you will search for the expected number y (x*r), if you find it, similar way you save the next value z = y *r in the v3. So, anything in v3, you know it is your second element. next time, when you see the value you z, you will get your expected triple... it is the way it works suppose 1,3,9,9,27,81 example. In the first iteration there is no value for count and v3, because right side v3[1] and v2[1] doesn't exists, but add 1 to the v2[3] . In the second iteration. you don't have any value for count as v3[3] doesn't exist. But you have v2[3] from previous iteration. So, now v3[9] = 1 and v2[9] = 1. so, the value v3[3] means you got 2 value for your triple. In the third iteration, count is 1 as you v3[3] has value from previous iteration and the triple are completed. Similiarly next moves are continued if you still don't understand, just print(k, v3, v2) at the end of your for loop, you will see the changes of each iteration and make sense.