---
# System prepended metadata

title: Max Slice in array

---

# Max Slice in array

- There is a given *non-empty* array A of N integers.
- We call a "slice" of array A, a pair of integers (i, j) such that 0 <= i <= j < N.
- The sum of the slice is the total of all values of array A between index i and j including both marginal values.

[-3,100,-10] => 100
[-3,1000,-10, 0, -500, 20, -1000, 1000, 1000, -1000] => 2000
[-6,-3,-10,-500,-1000, -100, -100] (max slice is 0)

Assumptions:
* Each element of array A is a positive or negative integer within the range [- 100 000, 100 000] 
* N is an integer smaller than 1 000 000.
* When the max slice sum is negative function should return 0.


```python=3.8
def max_slice(A):
    n = len(A)
    max_sum = 0
    for slice_length in range(1, n+1):
        for i in range(n-slice_length+1):
            current_sum = sum(A[i:(i+slice_length)])
```         if current_sum > max_sum:
                max_sum = current_sum
                best_i = i
                best_slice_length = slice_length
                
    return (best_i, best_i + best_slice_length)




# Party Problem

Consider a big party where a log register for guest’s entry and exit times is maintained. Find the time at which there are maximum guests in the party. Note that entries in register are not in any particular order.

Example Input: 
	arrivals = [1, 2, 9, 5, 5]
    exits = [4, 5, 12, 9, 12]
    

```python=3.8
def count_smaller(arr, hour):
    count = 0
    for i in arr:
        if i <= hour:
            count += 1
    return count
      

arrivals_sorted = np.array(arrivals).sort()
exits_sorted = np.array(exits).sort()

max_n = 0
hour = 0

for h in range(1, 13):
    n_arrivals = count_smaller(arrivals, h)
    n_exits = count_smaller(exits, h)
    n_party = n_arrivals - n_exits
    if n_party > max_n:
        max_n = n_party
        hour = h
        
return hour
    
    
```