Copy of Array 3 Annotated
Copy of Array 3 Annotated
ARRAY-3
Lecture- 14
Raghav Garg
Today’s checklist
1011 I
#111
- 4oz -
0 -> (noz-1* 0
=
th
nOZ -> I
Ques : Sort the array of 0’s and 1’s .
i
11 1 I I
V- ⑩0 0
O 1 2 3 4 S 6 7
n 8
=
100 0 &BYS
=
noz 0XX
=
R 2 3 Y S 6 7 int i 0
=
O O 0 O I I I I int
=n-1
&
I &
while (i<j)
if (avr (j) ==
1) j--;
front if (avr [iT =
0) i++;
- O's
=
is the back
if (arr[i] ==1&& arr(j] 0) =
=
are to
3
Swap();
swapping
i++;
j -
-;
Ques : Sort the array of 0’s and 1’s .
O O O I 1 I I I
&
L
I
I
if law (j)
==
1) j--3
If law (i)
=
=
0) i+ +
arr(iT 0; =
aur(j)=1;
i + +;
j --;
Ques : Sort the array of 0’s and 1’s .
8 00 O I I I I I
I
if (is j)
- >
break;
Ques : Move all negative numbers to beginning and
positive to end with constant extra space. (Classwork)
3 6 S -
Y 2
18
-
-
I
&
Ques : Sort the array of 0’s , 1’s and 2’s . (LeetCode 7S)
M-T
Algorithm
Dag
->
S
-
-
O ⑧ I I 2 2
3 Y
S
8 I 2
noz =
2
now: 2
notw= 2
Ques : Sort the array of 0’s , 1’s and 2’s .
M-2
-
3
pointer algorithm (dutch flag algo)
to mid hi
-
I n 1
hime
-
o-lo
2
O
nint - v0 lo-1
to
0 to to mid-1-1
~ With to note 2
mid + khelna e
if
Ques : Sort the array of 0’s , 1’s and 2’s . int 10 0
=
int mid=8
lo hi
int hi m-1=
O O I I 2 2
mich
1) If (nums (mid) 2)
==
2) if (nume(mid] 0)==
3) if(nums (mid) ==1)
swap (mid, his swap (mid, lot midt+;
lo+ t
hi--;
mid++
while (mid-high) [
3 conditions
3
Ques : Merge two sorted arrays . (LeetCode -
88)
aurs 101
arr2 7/107
aw3 s7(8/10
#
#Hint 3
-
pinters -
i, j, k
Ques : Merge two sorted arrays .
int arr3 [m+n];
518/ int i 0=
arrs
intj 0=
i
intR 0 =
as
szT;
as
intot
it's
R
Ques : Merge two sorted arrays .
int i m-1
=
~
518
arrs.
inti n-
=
intk mth-1=
1
warz s1zYTE
j
merars,
is to
it
R
**
Ques : Find the next permutations of Array .
Note :- If not possible then print the sorted order in
ascending order. (Leetcode 31) -
3
1, 2,
I 2-
I 42 3 ↓
idx
L
I 2 3 4
1432
2 I 34 v
Ques : Find the next permutations of Array .
Note :- If not possible then print the sorted order in
ascending order. pivot idea intidx=-1;
idX
I 2 E
U 3 for (inti n-2;
=
ix,0;i --
1
1
idy
I
I 2 3 U idx=ii
I 3 2 U break;
3
Find idx
Step- 1
-
fairot
3
Sep.
2 ->
*
idx+1 end reverse
-
Stefy swapping
3
sout idx, I
Step-3 ->
idxt to
end find just greater numberRa idy
- --
Ques : Find the next permutations of Array .
Note :- If not possible then print the sorted order in
ascending order.
U 3 2 1 + 1 2 3 4
if lidx==-1)[
reverse
idx
2 3 I
O I 2
2 I 3
Ques : Find the next permutations of Array .
Note :- If not possible then print the sorted order in
ascending order. ids
I 3
a
C
I
1 ③ 2
I 32 3 -
Ques : Trapping Rain Water (Leetcode -
42) (Hard)
Famous
Y 2 03 2 S
[010210132121]
#7
water-?
5 -
y =
3 -
2
=
1
-
Y 2 0 32 S
fever createst tent a Next Greatest Element
iT
-
In FI
heights (0 1 0 2 10 32 12
1]
heights (i] < mini (it
ar(-1000833,33]
bur (33 -
3333220-1)
car [-11011 22222211-1]
Rev. Gatest Element Away arr (0) =-1
i
heights (0 0210132121]
are [-10 1122223333]
max 0X &
=
3
it+ 12
for lint:= 1; i < 12;
arr (i):max;
if(max< heights (i)) max=
heights(i];
3
Next Greatest ElementAway:
&
heights (8 0210132121]
bor(33333332221 11]
max=
heights (n-1];
for lint i n-2;
=
is,0;i -)[
-
max y & 3
I
=
brr[i]= max;
12 pe -
1 O 1122223333
n +
-
1
max 0X 1
=
-
Trapping Rain Water…continued
height (010210132121]
max 1
=
[1 I
24*$B
0 1 1 2 2
per A
3
THANK YOU