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.
In this problem, you throw a stone x blocks far, and the next time you throw the stone x+1 blocks far, then x+2 blocks far and so on... The problem asks if you can reach the N-th block in this way. That means to know if a sum of integers can reach the value N. We know that the sum of the first x integers (from 1 to x) is given by the formula x*(x+1)/2. So we have to know if x*(x+1)/2 = N is true for an integer value of x. Solving for x you get the equation x^2 + x - 2N = 0, then x = (-1 + sqrt(1+8N))/2. If x is an integer you have a solution, otherwise you will be more lucky next time :)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Stepping Stones Game
You are viewing a single comment's thread. Return to all comments →
In this problem, you throw a stone x blocks far, and the next time you throw the stone x+1 blocks far, then x+2 blocks far and so on... The problem asks if you can reach the N-th block in this way. That means to know if a sum of integers can reach the value N. We know that the sum of the first x integers (from 1 to x) is given by the formula x*(x+1)/2. So we have to know if x*(x+1)/2 = N is true for an integer value of x. Solving for x you get the equation x^2 + x - 2N = 0, then x = (-1 + sqrt(1+8N))/2. If x is an integer you have a solution, otherwise you will be more lucky next time :)