Under construction

Still being built. Things may change

Minimum subarray average

Time limit1sMemory limit512 MB

Summary
Find the starting index of a length-two-or-more subarray whose average is smallest, breaking ties by smallest start.
Level

Medium6 of 10

Topics
Array, Math, Greedy
Solved
No attempts yet

Problem

You are given an array AA of length NN whose indices start at 0. For two integers PP and QQ with 0≤P<Q<N0 \le P < Q < N, the subarray average A(P,Q)A(P, Q) is defined as

A(P,Q)=∑i=PQA[i]Q−P+1A(P, Q) = \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1}

so a subarray average is the average of two or more consecutive elements.

For example, if N=3N = 3 with A[0]=3A[0] = 3, A[1]=1A[1] = 1, A[2]=2A[2] = 2, the possible subarray averages are A(0,1)=2A(0, 1) = 2, A(0,2)=2A(0, 2) = 2, and A(1,2)=1.5A(1, 2) = 1.5. The smallest of them is A(1,2)=1.5A(1, 2) = 1.5.

Given the array AA, find a pair (u,v)(u, v) whose subarray average A(u,v)A(u, v) is minimal and print uu. If several pairs reach the minimum, print the smallest uu.

Input

The first line contains the length NN of the array. The second line contains A[0]A[0], A[1]A[1], …\dots, A[N−1]A[N-1] separated by spaces.

  • 2≤N≤1,000,0002 \le N \le 1{,}000{,}000
  • 0≤A[i]≤7×1080 \le A[i] \le 7 \times 10^8

Output

Print uu on the first line.

Hint

If P+1<QP + 1 < Q, then there is an integer KK with P<K<QP < K < Q such that

∑i=PKA[i]K−P+1≤∑i=PQA[i]Q−P+1≤∑i=K+1QA[i]Q−K\frac{\sum_{i=P}^{K} A[i]}{K - P + 1} \le \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1} \le \frac{\sum_{i=K+1}^{Q} A[i]}{Q - K}

or

∑i=K+1QA[i]Q−K≤∑i=PQA[i]Q−P+1≤∑i=PKA[i]K−P+1\frac{\sum_{i=K+1}^{Q} A[i]}{Q - K} \le \frac{\sum_{i=P}^{Q} A[i]}{Q - P + 1} \le \frac{\sum_{i=P}^{K} A[i]}{K - P + 1}

Examples6

  1. Example 1

    Input
    3
    3 1 2
    
    Expected output
    1
    
  2. Example 2

    Input
    7
    4 5 2 2 1 5 8
    
    Expected output
    3
    
  3. Example 3

    Input
    2
    700000000 0
    
    Expected output
    0
    
  4. Example 4

    Input
    4
    0 0 0 0
    
    Expected output
    0
    
  5. Example 5

    Input
    5
    4 4 4 4 4
    
    Expected output
    0
    
  6. Example 6

    Input
    6
    1 2 3 4 5 6
    
    Expected output
    0