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.
well the process goes like this,
the question is to add the values to the array in between 2 indexes,
instead what we do is, at the start index, we add the number and after the end index we subtract the number.
and finally we loop it to make the sum of all elements in the array, we find the maximum sum possible.
and the logic is,
here key is to find the maximum sum, but not generating the array, so when we add a number at start index, all the consecutive sums have the number in it's sum and when we subtract it we are actually removing the number so it does not exists outside the end index. by looping and finding the sum gives the maximum number.
Hope my explination helped...
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
well the process goes like this, the question is to add the values to the array in between 2 indexes, instead what we do is, at the start index, we add the number and after the end index we subtract the number. and finally we loop it to make the sum of all elements in the array, we find the maximum sum possible. and the logic is, here key is to find the maximum sum, but not generating the array, so when we add a number at start index, all the consecutive sums have the number in it's sum and when we subtract it we are actually removing the number so it does not exists outside the end index. by looping and finding the sum gives the maximum number. Hope my explination helped...