File size: 774 Bytes
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*@ Import Lean
import Algorithms.maximum_subarray.lean.spec_lib
open scoped SimpleC
*/

/*@ Extern Coq
      (MaxSubarraySumPrefix : list Z -> Z -> Z -> Prop)
 */
int max(int a, int b)

{
    return (a > b) ? a : b;
}

int max_sub_array(int *arr, int n)
/*@ With (l : list Z)
    Require
      1 <= n && n <= 100000 &&
      Zlength(l) == n &&
      IntArray::full(arr, n, l) &&
      (forall (k : Z), (0 <= k && k < n) => (-10000 <= l[k] && l[k] <= 10000))
    Ensure
      MaxSubarraySumPrefix(l, n, __return) &&
      IntArray::full(arr, n, l)
 */
{
    if (n == 0) {
        return 0;
    }

    int cur = arr[0];
    int res = arr[0];

    for (int i = 1; i < n; i++) {

        cur = max(arr[i], cur + arr[i]);

        res = max(res, cur);

    }

    return res;
}