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.
No, that's not true. In this case, i will be 0 at the end (not -1). That's because i-- is executed only if sides cannot form the triangle.
Moreover, this extra case is flawed. Because if there's no possible triangle, then the value of i is -1, and hence, program will be accessing arr[-1] i.e. accessing memory outside the bounds of the array, which can give unexpected results anytime.
Anyway, you don't need to add any extra check. Just modify if(i>0) to if(i>=0) and the program will execute correctly !
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Perimeter Triangle
You are viewing a single comment's thread. Return to all comments →
No, that's not true. In this case, i will be 0 at the end (not -1). That's because
i--
is executed only if sides cannot form the triangle.Moreover, this extra case is flawed. Because if there's no possible triangle, then the value of i is -1, and hence, program will be accessing
arr[-1]
i.e. accessing memory outside the bounds of the array, which can give unexpected results anytime.Anyway, you don't need to add any extra check. Just modify
if(i>0)
toif(i>=0)
and the program will execute correctly !