0% found this document useful (0 votes)
7 views13 pages

Problem Solving Session

The document outlines various algorithms related to array manipulation, including finding equilibrium indices, leaders in an array, and subarrays with a given sum. It discusses the implementation details, time complexity, and space complexity for each algorithm. Key concepts include prefix sums, counting leaders, and using sliding windows for subarray sums.
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)
7 views13 pages

Problem Solving Session

The document outlines various algorithms related to array manipulation, including finding equilibrium indices, leaders in an array, and subarrays with a given sum. It discusses the implementation details, time complexity, and space complexity for each algorithm. Key concepts include prefix sums, counting leaders, and using sliding windows for subarray sums.
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
You are on page 1/ 13

Agenda

In place prefix sum


21 Equilibrium index
31 Leaders in an array
subarray of length is with given
4 sum

In place prefix sum

A 1 4 5
3
s c

into ps array only when allowed

A 1
i Ali ACI Ali I
a Given an AI of equilibrium indices
find count in it

i is an equilibrium index if sum of elements on its


note

left is same as
sum
of elements on sight

4 ans 2
A 1 9 3

A 0 I 1 3 2 ans I
0 I 2 3 4

A 1 ans a
7 4

sum
of left sum
of right
to i i ith to n t

i i Pstn i ps i
junction solve 1 A

I constra it ps n

count 0

for 1 it to
0 n t

15 0

if i O
T1 O n
Is ps i i
3 Sc

85 ps n 1 ps i
get rid of
if 115 851 extra space
without change
lounttt
3
All I

A 1 9
T.it kEuy

Is 041 140 as 4441 44 to X 0 24


Y f 0

count 072
Junition solve AC

count 0

15 0 85 0

1185 1 total sum initially


for it o to n t

rst Ali
3

for lino to n 1 I n
85 Ali
18

ijlds 851 45 0 1 173 2 85 0 4 3 4


lountt 1
4 Y Y y
3

1st Ali count


01
3

return count
3

T1 O n

51 O 1
AI find total no of leaders A leader is
G Given an
an element strictly greater than everything on its right
Note AC n t is always a leader

ans 3
A 118 1,7 9

A 9 5 8 ans 3
9

A 1 ans 1
9

Junction leaders A1 msf max so far


11 max R to L
Msj A n 1 so far
11 1 11 leaders count

it to 0
for n z

if Ali msg
TC O n
ditt SC OCI

3 may Asi
3

3 return is
A1 i
Junction leaders
A 118 1,7 9
Msg A n 1 11 max so far
11 1 11leaders count

for it n z to 0 Msg It is 1
if Ali msg
ditt

3 may Asi
3

3 return is
A an 2
118 1,7
4,329
A ans 3
19
If
A 4 2 3 9 7 10 an 8
0

A 5 4 3 2 ans O no transaction
0 1 2 3
A 1

msg min so far


msJ 4 2 trans 10 2
L to R
7 8
max profit

Junction solve A1

A 0 I min so far
msj
max profit 0

11 trying everyday as selling day


Joo in 1 to n it

trans Ali ms

TC O n
if transs max profit
trans SC 011
max profit
3

if Ali msg
Msg Asi
3
3

return max profit


3
a Given an AC k s s Find if there is a subarray of length
s
k whose sum is equals to
4 4 5 9 1
A 1 4
5

3 5 12 0
A 1 9

Ending
windo s e
a 1
K 4
5 11

s e sum

0 3 4

4 4 I 6 9
I

2 5 9 5 4 8

3 6 8 0 3 11
function solve A1 K s

I Jinding sum of 1ˢᵗ window


sum 0
it 0 to k l
for
sumt Ali
3

if sum 5 9
return 2
5

I let's go on remaining windows


TC O n
5 1 e IC
51 011
while le ni

same sum Als 1 Ale

if sum s

return 7
3
stt ett
3

return 0
doubts
0 1 2 n 3
0 I 0
0 2 3 0

516
1
2 4 0

7 784 3

6
5

8
7

9 0 0

A 1

max Ali
i o
its i value after its

assume there are 11 its 3

after k its
whifftops
i
k

N 2

dog on both sides


logan iterations

dog N logz2

RgzN

You might also like