Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd1f146

Browse files
Update Tutorial.md
1 parent 3c855c4 commit bd1f146

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎ArraysAndStrings/Tutorial.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,31 @@ public static String replaceSpaceWithPercent20(String str) {
145145
return sb.toString();
146146
}
147147
```
148+
149+
**Given an array of integers, find subarray with maximum sum**
150+
```
151+
public void maxSubArraySum (int arr[]) {
152+
int maxSoFar = Integer.MIN_VALUE;
153+
int maxEndHere = 0;
154+
int start = 0;
155+
int end = 0;
156+
int s = 0;
157+
158+
for (int i=0 ; i < arr.length; i++) {
159+
maxEndHere += arr[i];
160+
if(maxSoFar < maxEndHere) {
161+
maxSoFar = maxEndHere ;
162+
start = s;
163+
end = i;
164+
}
165+
if (maxEndHere < 0) {
166+
maxEndHere = 0;
167+
s = i+1;
168+
}
169+
}
170+
System.out.println("maximum sum of subarray-"+maxSoFar);
171+
System.out.println("starting index-"+start);
172+
System.out.println("ending index-"+end);
173+
}
174+
```
175+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /