0% found this document useful (0 votes)
25 views10 pages

Maximum Subarray Sum Algorithms

The document discusses various algorithms for finding maximum subarray sums, including brute force, prefix sums, and Kadane's algorithm. It outlines the implementation details and time complexities for each method, emphasizing the efficiency of Kadane's algorithm. Additionally, it briefly touches on problems related to trapping rainwater and finding the best time to buy and sell stocks.

Uploaded by

apna jaat gaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

Maximum Subarray Sum Algorithms

The document discusses various algorithms for finding maximum subarray sums, including brute force, prefix sums, and Kadane's algorithm. It outlines the implementation details and time complexities for each method, emphasizing the efficiency of Kadane's algorithm. Additionally, it briefly touches on problems related to trapping rainwater and finding the best time to buy and sell stocks.

Uploaded by

apna jaat gaming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

① maximum sub

array Sum .

(Brute force)
>
- Here we need two for loop ,

Outer loop is the starting index


of Subarray
Inner loop for traversing the
Subarray
every time and counting the Sum
.

*
Here we need two variable , ie-current Jum
max sum
* Current Sum help us to calculate the
Aum
current of Gebarray
* And maysum win store the maximumSum
of each sub
array
So we all
compared Maximum Sum with
ayssum
cutient

code
- // function definition (
int maySumnegerMIN-VAL ,
for (int i to ; i < nums .

Length ; i+ + &
for (inty i ; i < nums
= ·
Length ; j ++ ) &
for (intr i = R < j =
; k+ + ) [
Current Sur + nums (K] ;
-
=

if (Maxsum <Current Sum) [


MaySum =
Current sum ;

Current tum = o
;
S
system. out .

Printin ("may Sub


Array Sur : "T
may sum);
S


/prefine a
ommSun subarray
31 , 9 , 7 , -1 ,

& then prefinetrad showed be

&
Do Can Achave this
by travcise the original Array
If we look at the Element of prefin Array ,
PrefireArray <is =
PrefinArray (i-1) + array [i] ;

After that instead of 3 for loop We have to declare .

2 for loop which timeComplexity 0 (n2 < 0 (43) =

ra And the
SubArray Sum can be found tha
by
Current Sum =
Start == 0 ? Prefin[end] :

Prefixe [end] -Prefie (start-1] ;


· function definition array ,
da

int Current sum = o


; maxSum =
Integer -

MIN VALVE ; -

Prefixe [03 =

array [0] ;

for (int i = s ; is array -

Length ; it + E
Prefix (i) =
Prefies [i-1) + array (i) ;
&

for (int i = o
; i <
array ·

Length ; i +) &
for (int j = i; jc array ·

Length ;
j ++) &
Current Sum = i = = 0 ? Prefixe (j) :

Prefixe (end]-Prefix [Start-1) ;


if (maybem < CurrentSum)[

maxsum-Current Sum ;
S
3
3
printe ("May dry "I may du)
system
. out Sub Aray :
③ may subArray sum .

(Kadane's Algorithm
This the +ve + Ve
Algorithm days that, =

+Ve + -ve-tve
be initialize tor Variable Ye ,

tre =Ve
+ = -ve

CurrentSum
>
= o

and maysum = -2

*
and one
for loop that traverse the Array only one time
We CalculateCurrent
Sur , After Added the
*
and
Element to theCurrent ,
or
should cheek that
Sum
of sury is Stir less than zero then assign
,
Zero
to Current
Sum instead of negative .
number

They
# the
Curreyson is (positive
the

*
compare mayvalue
ser with Current Sum
to
and Store
maximu may dum
* At last return may sum ;
* This Algorithm not gonna Dork if the , all

Element in the Array negative (-ve]


are

# To Dork this
Should be attest Algorithm on an
Array , there
the
one the Element in
Array
-
Trappingwater
R
Here be need I
may
Variable , e

int
lettmax =

Integer
·
MIN VALUE ;
-

int
Rightmax
:

Integer ·

MIN VALUE;
-

and two Array that stored the may value


From
leftSide and Right Side

int leftmane ,
raylt
In
>
-

leftmayA we
the
giv
arign
the
maximum bar value

per
as iteration from left to right
Similarly I ,
righttherayB bediv
assign the Maximum
bar

por iteration from


right to left
value as

After foryed both the .


Array
a
overBothhe
Againutoterate Arrayatat
e

and Subtrac tubuheight the


ina
by respective se

and Add the Value


After subtraction to a Variable

and return that Variable.

* Topping
in D
Code for rain water
trapped
" function definition [int Watertrapped =
o
;
int
leftgax Integer MIN VALUE ;
= ·

int
Rightmax Integer MIN-VALUE ;
= ·

int is
lettray Array =
new
new
int
Carray Length) ; -

int[] Night max Array =

int(array Length); .

for (int i =
o
;
i <
away
·

length ; it +) [
if (leftmax <away <is) [
left may =

away [i] ;
3
3
LeftmayAray [i] =
leftmay ;

for (int j =

array
·

Length -
1 ;
y
= 0
; j--)[
if (Rightgax <
away <j]] [
rightnay =
array [J] ;

Right mayArray (j)


=

rightma ;

for (intK = 0 ; <


array Length
· : k + +) &
int minimum-Math -
min (leftraxArray [*](K]) ; ,

RightmaxArray

S
patertrapped + =

minimum-arrays ;

> System. out .

Paintin ("May Cater can trapped "Gratetrapper :


* Best time toBuy and So
To find out the best time to buy stock it ,

should
be

Minimur price at ,
all

At first have to findout the minimum


so We
price of the
-

array that given


the stock
price.
->
After found the minimin Stoeprice we should
,
return
the
index of that minimum stock ,

They have to iterate over the Aray


-
Do Crice Fran the
minimu
indea to end to findout the maximu
Stook
in
,

price whicha see our


can stock

After found the


maxigue stock price beLiv Subtract
,

the Stoor Price Value by the minimum Price


maxing .

and return the profit


7
Best time to buy and see the steam :

47 ,
2 ,
5 ,
3 ,
6 ,
43
·

In
indeo ber
greater
than 7 in
G21513 6,4) ,

~
I

inded Number Afte e


5
· I profit : 6-1 =

inde
D Number After 5 =

Profot 6-5-
inde
3
:

6
Number After 3 =

profit = 6-3 ③ =

IndeMiam
At- a

profit=
o

You might also like