# Array manipulation, difference between array and array object

**URL:** https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237
**Category:** programming
**Created:** [June 10, 2020, 10:45am UTC](https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237 "2020-06-10T10:45:01Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)
#### Post date: [June 10, 2020, 10:45am UTC](https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237/1 "2020-06-10T10:45:01Z")

</div>

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 🙂

note: this is one of the data structure problem at,[https://www.hackerrank.com/challenges/crush/problem](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`

```auto
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

```auto
    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

    }

```

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [June 10, 2020, 12:49pm UTC](https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237/2 "2020-06-10T12:49:41Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![Umair\_Ashraf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/umair_ashraf/32/11705_2.png) [@Umair\_Ashraf](https://forum.kirupa.com/u/Umair_Ashraf)
#### Post date: [June 10, 2020, 2:44pm UTC](https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237/3 "2020-06-10T14:44:04Z")

</div>

Thanks, yes it does make sense now 🙂

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

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/2X/d/d1fa55815861365199298d7a1692877418d912d0.png)

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [June 10, 2020, 3:16pm UTC](https://forum.kirupa.com/t/array-manipulation-difference-between-array-and-array-object/643237/4 "2020-06-10T15:16:31Z")

</div>

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.
