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.

Java solution
I just copied on what the idea from others as my solution is slow. After studying and understanding here's what I understand on this solution.

Idea and Concept:
1. Just like the common and longer solution, still create an array to determine the highest number.
2. Instead of looping through the start and end indexes, we approach a different method using the start and end only leaving the middle indices 0
3. Let's explain the concept of why arr plays a significant role:

To fully understand let's follow the given values similar to case 0:
n = 5
queries = [[1, 2, 1], [2, 5, 5], [4, 5, 10]]

before the first loop, the arr would be like this:

arr=[0,0,0,0,0,0,0];// [n + 2] = 7

After first loop what happens will be like this:

arr=[1,0,-1,0,0,0,0];

since what we does is get the a then make it as base-0 array that's why arr[startIndex - 1]

The question is why is there a negative value and on top of it it's supposed to be on the first and second index ([1, 2, 1]), I'll explain later.
Let's wrap up the final value of arr after the whole loop, which would look like this

arr=[1,5,-1,10,0,-15,0];

For the second part, this is where everything makes sense, the idea is like this:

There should be a variable (currentNumber) that will scan over each element and the purpose of it is to determine and add together the numberes and determine the highest number.
First element is 1

base on the query there is 1 and 5 in this element and that is the purpose of currentNumber to remember that this is the total on to where the value is supposed to be added.

So as of now the highest value is 6 which is 5 and 1

Third element is -1

currentNumber = 5 -> add -1
This is where the negative plays an important role is informing the currentNumber that the value 1 stops here, and thus neutralizing the currentNumber so now it becomes 5 which is still part of the second query [2, 5, 5]

And so on, until you reach the 4th element which is 10, base on the query this is where 10 should be added, but our currentNumber = 5 since we haven't neutralized or in other words 5 is still should be there, therefore you add the two which will be our highest number.

There are far more better ways to explain this, but this is for now.

## Array Manipulation

You are viewing a single comment's thread. Return to all comments →

Java solution I just copied on what the idea from others as my solution is slow. After studying and understanding here's what I understand on this solution.

Idea and Concept: 1. Just like the common and longer solution, still create an array to determine the highest number. 2. Instead of looping through the start and end indexes, we approach a different method using the start and end only leaving the middle indices 0 3. Let's explain the concept of why

`arr`

plays a significant role:To fully understand let's follow the given values similar to case 0: n = 5 queries = [[1, 2, 1], [2, 5, 5], [4, 5, 10]]

before the first loop, the arr would be like this:

After first loop what happens will be like this:

since what we does is get the

`a`

then make it as base-0 array that's why`arr[startIndex - 1]`

The question is why is there a negative value and on top of it it's supposed to be on the first and second index (

`[1, 2, 1]`

), I'll explain later. Let's wrap up the final value of`arr`

after the whole loop, which would look like thisFor the second part, this is where everything makes sense, the idea is like this:

There should be a variable (

`currentNumber`

) that will scan over each element and the purpose of it is to determine and add together the numberes and determine the highest number. First element is`1`

base on the query there is

`1`

and`5`

in this element and that is the purpose of`currentNumber`

to remember that this is the total on to where the value is supposed to be added.So as of now the highest value is

`6`

which is`5 and 1`

Third element is

`-1`

`currentNumber = 5 -> add -1`

This is where the negative plays an important role is informing the`currentNumber`

that the value`1`

stops here, and thus neutralizing the`currentNumber`

so now it becomes`5`

which is still part of the second query`[2, 5, 5]`

And so on, until you reach the 4th element which is

`10`

, base on the query this is where`10`

should be added, but our`currentNumber = 5`

since we haven't neutralized or in other words`5`

is still should be there, therefore you add the two which will be our highest number.There are far more better ways to explain this, but this is for now.