# [Solutions to problems] Arrays & Dynamic Arrays

May 28, 2020

## Transcript

1. 1.

2. 2.

3. 3.

### Arrays Deﬁnition Collection of values of the same type, stored

in contiguous memory, accessible by an index.
4. 4.

5. 5.

6. 6.

### 2-dimensional array ✅ Values of the same type • First

dimension stores values of type: “arrays of 5 integers”. • Second dimension stores values of type: “integer”
7. 7.

8. 8.

9. 9.

10. 10.

### 2-dimensional array ❓ Accessible by and index A[i][j] = ?

Hint: For a 1-dimensional array , the i-th element can be located at address A A[i] = S + ti
11. 11.

### 2-dimensional array ❓ Accessible by and index A[i][j] = ?

Solution: For a 2-dimensional array , an A A[i] = S + it1
12. 12.

### 2-dimensional array ❓ Accessible by and index A[i][j] = ?

Solution: For a 2-dimensional array , an element on the 2nd dimension, of type , can be found at: A t2 A[i][j] = S + it1 + jt2
13. 13.

### 2-dimensional array ❓ Accessible by and index A[i][j] = ?

Solution: For a 2-dimensional array , an element in the 2nd dimension, of type , can be found at: A t2 A[i][j] = S + it1 + jt2 If our were to start at memory address 800 (in decimal), then: A
14. 14.

### 2-dimensional array ✅ Accessible by and index A[i][j] = S

+ it1 + jt2 *Getting and setting time complexity remains O(1)
15. 15.

### Multi-dimensional array Indexes • 2 dimensions: • 3 dimensions: •

4 dimensions: A[i][j] = S + it1 + jt2 A[i][j][k] = S + it1 + jt2 + kt3 A[i][j][k][l] = S + it1 + jt2 + kt3 + lt4
16. 16.

⋅ 2 = 40
17. 17.

### Multi-dimensional array Size Let be the number of dimensions of

an array, an speciﬁc dimension such that , and the size of the dimension , therefore the size of an array is: Or simpliﬁed: d s 1 ≤ s ≤ d ns s n1 ⋅ n2 ⋅ n3 ⋅ . . . ⋅ nd d ∏ s=1 ns
18. 18.

19. 19.

### Multi-dimensional array Size The i-th dimension have size: Or simpliﬁed:

ni ⋅ ni+1 ⋅ ni+2 ⋅ . . . ⋅ nd d ∏ s=i ns
20. 20.

### Multi-dimensional array Size A[i1 ] . . . [id ]

= S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id
21. 21.

### Multi-dimensional array Size A[i1 ] . . . [id ]

= S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id If we say that then: d ∏ s=d+1 ns = 1 d ∑ t=1 d ∏ s=t+1 ns
22. 22.

### Multi-dimensional array Size A[i1 ] . . . [id ]

= S + i1 d ∏ s=2 ns + i2 d ∏ s=3 ns + . . . + id−2 d ∏ s=d−1 ns + id−1 nd + id If we say that then: d ∏ s=d+1 ns = 1 d ∑ t=1 d ∏ s=t+1 ns
23. 23.

### Python Challenge github.com/platzimaster/challenge-python-09 Given an array of numbers between of

length , modify the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0 Example 1 • Input: [1, 0, 1, 2, 9, 0, 1] • Output: [1, 0, 0, 1, 2, 9, 0] Example 1 • Input: [1, 2, 3] • Output: [1, 2, 3] Note: Found a solution with O(n) in time and O(1) in space complexity
24. 24.

### Given an array of numbers between of length , modify

the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0 Test case • Input:  • Output:  Test case • Input:  • Output:  Test case • Input: [0, 0, 0, …, 0] • Output: [0, 0, 0, …, 0] Test case • Input: [1, 2, 3, 4, 0] • Output: [1, 2, 3, 4, 0]
25. 25.

### Given an array of numbers between of length , modify

the given array (in place) such that every appears twice while maintaining the size of the array. A [0, 9] 1 ≤ n ≤ 109 0
26. 26.

### Meaning of criteria • : Asume a computer can perform

primitive operations in 1 second, a solution will perform primitive operations, which will take seconds or years. Which implies a (or less) solution. • In place: Means no additional/auxiliary space is required. Which implies a space solution. • Maintaining the size: Array size must not change, and every number pushed outside the array will not be part of the array anymore. 1 ≤ n ≤ 109 109 O(n2) 1018 109 ≈ 31.7 O(n) O(1)
27. 27.

### Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n]
28. 28.

### Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n] O(n)
29. 29.

### Brute force approach (Following problem description) def duplicate_zeros( ): ⃪

len( ) for ⃪ : if == 0: .insert( , 0) return A n A i {0,...,n − 1} A[i] A i A[: n] O(n) O(n2)
30. 30.

### Observations • Every modiﬁcation from left to right costs •

For every 0 we found, we loose one element from the end • If the last element is 0, there’s nothing to do O(n)
31. 31.

### Optimum solution • Count the number of zeros that will

be duplicated, as • Read from using index • Set • If : • Decrease by • Set to d n − d − 1 i A[i + d] = A[i] A[i + d] = 0 d 1 A[i + d] 0
32. 32.

33. 33.

34. 34.