Ad
  • Custom User Avatar

    Your solution fails following tes cases:

    a=50095301248058391139327916261
    b=81055900096023504197206408605
    
    a=6144617321112760
    b=4344202181817737
    
  • Custom User Avatar

    I may be misunderstanding the question - but you do know the contents of the array, as this is passed into the function you are making.

    Arrays can be accessed in most languages like so - assumeing I have an array called "MyArray"

    MyArray[0] - this would grab the first value of the array, MyArray[2] would grab the 3rd and so on.
    You can find the length of the array in - for example C#, by using MyArray.Length

    So with this in mind, you should be able to loop through an array and find the max sum

    I agree with the poster above me - it would help to draw an array on a piece of paper, and figure out how you would do this manually.

  • Default User Avatar

    Yes, once you understand the objective of the kata, you have to come up with an algorithm/program that finds the result yourself!

    As always, if you are a bit confused, it is a good idea to try with the given example and use a pen and paper (old school) before trying to write code - how would you solve this kata on paper in "English/words".

    For your 2nd question, I don't understand 100% - the "length of the optimum subarray" can range from 0 to L (where L is the size of the input) - for example:

    [-3,-6,-1] -> for this given input, the correct answer for the maximum subarray sum is 0; this occurs when you take the empty subarray [] with no elements in it (any other subarray would contain a negative number so it would be less than 0)

    [5,9,100,5,2] -> for this given input, the correct answer for the maximum subarray sum is 5+9+100+5+2=121, when you take the entire array of length 5.

  • Default User Avatar

    Hi - you have to find the maximum sum you can make from the given array, by selecting a contiguous (this means, "all side by side in the original array") subarray.

    For example:

    # Good example from description:
    [-2, 1, -3,     4, -1, 2, 1,    -5, 4]
    # should be 6: [4, -1, 2, 1] <---- note how this subarray IS CONTIGUOUS in the original input array
    
    # Bad example:
    [-2, 1, -3, 4, -1, 2, 1, -5, 4]
    #    ^______^______^__^______^  
    # these sum to 1+4+2+1+4 =12 BUT THE NUMBERS ARE NOT SIDE BY SIDE in original array
    # therefore this is NOT allowed