---
# 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
[-10, 3, -1, 100,-10] => 102
[1,100,-10] => 101
[1, -1, 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 solution(A : list) -> int:
    max_sum = 0
    for i in range(len(A)):
        sum = 0
        for j in range(len(A) - i - 1):
            slice = A[i:j]
            sum += A[i + j]
            #sum = sum(slice) if slice else 0
            max_sum = sum if sum > max_sum else max_sum
    
    return max_sum
        

```










# 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

from collelction import Counter

def solution(arrivals : list, exits : list) -> int:
    def fun(elem : tuple) -> int:
        return elem[0]
    
    count_arrivals = Counter(arrivals)
    count_exits = Counter(exits)
    
    diff = count_arrivals.subtract(count_exits)
    
    max_gusets_time = 0
    max_guests = 0
    current_guests = 0
    for key, value in sort(diff.items(), key=fun):
        current_guests += value
        max_guests = current_guests if current_guests > max_guests else max_guests
        max_guests_time = key if current_guests > max_guests else max_guests_time
    
    return max_guests
        
```