-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommercials.java
42 lines (33 loc) · 891 Bytes
/
commercials.java
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
package programming_challenges;
import java.util.Scanner;
public class commercials{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
int p = in.nextInt();
//move to next line
in.nextLine();
//store the commercials
int[] commercials = new int[n];
for(int i=0; i<commercials.length; i++){
commercials[i] = in.nextInt()-p;
}
//print profit
int max = maxSequence(commercials);
System.out.println(max);
}
in.close();
}
//Kadane's algorithm
public static int maxSequence(int[] passed){
int max_so_far = 0;
int max_ends_here = 0;
for(int i=0; i<passed.length; i++){
max_ends_here = max_ends_here + passed[i];
if(max_so_far < max_ends_here) max_so_far = max_ends_here;
if(max_ends_here < 0) max_ends_here = 0;
}
return max_so_far;
}
}