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.
according to the problem statement, it is only considered a triplet if i < j < k, from left to right, so [4, 2, 1] with r = 2 isn't a triplet.
consider this array with a triplet: [1, 2, 4]. if you traverse it normally, you won't know if you have a triplet until you get to the third element: 4. in order to confirm this, you would check that the second element is 4/r, and that the first element is 4/r/r. this uses division
the first bullet point in the example he gave states that he wants to avoid division (because it's expensive and can produce floating point numbers). when the array is traversed in reverse, in order to check if you have a triplet, you check that the next element was 4*r, and that the one after that was 4*r*r
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 →
according to the problem statement, it is only considered a triplet if i < j < k, from left to right, so [4, 2, 1] with r = 2 isn't a triplet.
consider this array with a triplet: [1, 2, 4]. if you traverse it normally, you won't know if you have a triplet until you get to the third element: 4. in order to confirm this, you would check that the second element is 4/r, and that the first element is 4/r/r. this uses division
the first bullet point in the example he gave states that he wants to avoid division (because it's expensive and can produce floating point numbers). when the array is traversed in reverse, in order to check if you have a triplet, you check that the next element was 4*r, and that the one after that was 4*r*r