GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java/Programs/Games/MaximumSubArraySum.java at main · arshcoder13/Java

Expand file tree

/

MaximumSubArraySum.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
45 lines (35 loc) · 1.52 KB
/

MaximumSubArraySum.java

Copy path

File metadata and controls

45 lines (35 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class MaximumSubArraySum {
//Maximum Subarray Sum using Divide and Conquer algorithm
public static int MaximumSum(int[] Arr, int firstindex, int lastindex) {
if (lastindex == firstindex) {//if the first index and the last index of array are same
return Arr[ firstindex ];//it means array contains only one element
//and this element equals maximum subarray sum.
}
// Finding middle element's index of the array
int middle = ( firstindex + lastindex ) / 2;
// Finding maximum subarray sum for the left subarray
int leftMax = -1000000000;
int sum = 0;
for (int i = middle; i >= firstindex; i--) {
sum += Arr[ i ];//addition for sum
if (sum > leftMax) {
leftMax = sum;
}
}
// Find maximum subarray sum for the right subarray
int rightMax = -1000000000;//should be minimum value
sum = 0;
for (int i = middle + 1; i <= lastindex; i++) {
sum += Arr[ i ];//addition for sum
if (sum > rightMax) {
rightMax = sum;
}
}
// Recursive part
int maxLeftRight = Math.max (MaximumSum (Arr, firstindex, middle),//left part of the array
MaximumSum (Arr, middle + 1, lastindex));//right part of the array
// return maximum of the three
return Math.max (maxLeftRight, leftMax + rightMax);
}
C:\Users\Ayse\Desktop\Java\Programs\Games\ MaximumSubArraySum
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.