-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject20.java
More file actions
51 lines (41 loc) · 1.26 KB
/
Project20.java
File metadata and controls
51 lines (41 loc) · 1.26 KB
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
45
46
47
48
49
50
51
import java.util.Scanner;
public class Project20 {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of values you want in the Array");
int n= sc.nextInt();
int[] A= new int[n];
System.out.println("Enter the value for variable K");
int K= sc.nextInt();
System.out.println("Enter the first value for A :- ");
for(int i=0;i<n;i++){
A[i]= sc.nextInt();
System.out.println("Enter the next Value :- ");
}
for(int i=0;i<n;i++){
System.out.println(A[i] + " ");
}
int[] B= new int[(n*K)];
for (int i = 0; i < K; i++) {
for (int j = 0; j < n; j++) {
B[i * n + j] = A[j];
}
}
System.out.println("Maximum subarray sum of B: " + Subarray_max(B));
}
static int Subarray_max(int B[]){
int size= B.length;
int max_so_far= Integer.MIN_VALUE;
int max_ending_here= 0;
for(int i=0;i<size;i++){
max_ending_here= max_ending_here + B[i];
if(max_ending_here > max_so_far){
max_so_far=max_ending_here;
}
if(max_ending_here<0){
max_ending_here=0;
}
}
return max_so_far;
}
}