Array manipulation, difference between array and array object

Hi guys,
i am trying to add two values at a specific index and places it back into an array, but the compiler is coming up with some Run time error… not sure if i am doing it correct.

also if any one can clarify please if array and arrayobject are the same things or there is any key difference between them.

appreciate your help :slight_smile:

note: this is one of the data structure problem at,https://www.hackerrank.com/challenges/crush/problem

so basically, i have two arrays,
// new1 is array
new1=[0,0,0,0,0,0]

// queries is array object

1 2 100
2 5 100
3 4 100

so basically, from first queries the x=1 (start position), y=2 (end position), z=100, so adding value z from position x to y in new1 array, and this will happen three times as we got three items in the queries.
so the working will be like…
new1 =[0 100 100 0 0 0 ] // here adding 100 to position 1 to 2
new1= [ 0 100 200 100 100 100] // here adding 100 to position 2 to 5
new1 = [0 100 200 200 200 100] // here adding 100 to position 3 to 4

    function arrayManipulation(n, queries) {
// n is the length of new array
    let new1 = new Array(n).fill(0)

    for( let i=0; i<n; n++){
    let x =queries[i][0]
    let y= queries[i][1]
    for (x; x<y; x++){

    let z =queries[0][2]
        new1[i] = new1[i] + z
    }


    }
    return new1

    }

Arrays are objects, so if someone says “array object” its the same as just “array”.

As for your code, you’re on the right track, but you have a number of little mistakes.

First, your initial loop should be looping through queries, not n. And the increment should increment i not n. As it is currently, it would create an infinite loop and cause havok.

for(let i=0; i<queries.length; i++){

Additionally, hackerrank says the indexing is 1-based, not 0-based which JavaScript uses. This means the indexes in queries are going to be off by one. When a query says 1, it actually means 0 in the JavaScript array. To fix this, x and y should be offset by one.

While you’re at it, you should bring up z too. It doesn’t need to be in the inner loop. It also should be based on i, not using queries[0] for each iteration.

let x = queries[i][0] - 1
let y = queries[i][1] - 1
let z = queries[i][2]

For your inner loop, you need to also include the y index. So 1 2 100 means both 1 (or 0 in JS) and 2 (or 1 in JS). To do this, the loop should check x<=y.

for (x; x<=y; x++){

Note: the initial x; isn’t necessary because it doesn’t do anything, but its also not hurting anything that its there. If you wanted to remove it, you could.

for (; x<=y; x++){

Finally, you’re adding to new1[i] when what you want is the indices of the queries array. That’s being defined by x, not i, so that’s what you should be referring to in new1

new1[x] = new1[x] + z

This can also be shortened using the += operator

new1[x] += z
1 Like

Thanks, yes it does make sense now :slight_smile:

it is working for most of the test cases, but it is failing 7 out of 15 Test cases onHackerrank.(Error: Your code did not execute within the time limits)
the initial though on my mind was that it might be the Math.max function that i am using to get the max value out of the new1 array but then i used .reduce method but still the same, i think Big O is the concern here as i understand r but not sure what else can i do to optimise here.

Any thought on this please.
the default timeout for javascript on hackerrank is 10sec.

the below is the code that i am using now and is passing 8 out 15 Test case.

function arrayManipulation(n, queries) {


let new1 = new Array(n).fill(0)

for( let i=0; i<queries.length; i++){
let x =queries[i][0]-1
let y= queries[i][1]-1
let z = queries [i][2]

for (x; x<=y; x++){

new1[x] = new1[x] + z

}


}

let max =new1[0]
let newfinal = new1.reduce(function (max,val) {if(val>max) max=val; return max},max)


//let newfinal= Math.max(...new1)



return newfinal

Error screenshot

My guess is that they’re feeding you some weird values that is causing problems. I just noticed its categorized as hard in difficulty. While the problem itself was pretty simple to solve, I image what’s making it hard is the complication around how their calling it and with what data.

1 Like