0% found this document useful (0 votes)
10 views27 pages

Arrays 03 Class Notes

Uploaded by

kmona2343
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)
10 views27 pages

Arrays 03 Class Notes

Uploaded by

kmona2343
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/ 27

C++

ARRAY-3
Lecture- 14

Raghav Garg
Today’s checklist

1) Problem solving on Arrays


Ques : Sort the array of 0’s and 1’s .
int n;
0 1 2 3 4 5 6 7

#111
1011 I
- 4oz -

M-I:Two pass method


->

int noz=0; A&BY

int now: 0 ; AR BY

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
=

Total time taken - In


Ques : Sort the array of 0’s and 1’s . int x
ptr x
*
M=
2:Two pointers "s two variables
--
-

R 2 3 Y S 6 7 int i 0
=

n-1
O O 0 O I I I I int
=
&

I &

while (i<j)
if (avr (j) ==
1) j--;
Hint O's are to the front if (avr [iT =
0)
=

i++;
-

is to the back
if (arr[i] arr(j] 0) ==1&& =
=

are

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+ +

iflav (i) == 1 & & arr(i] =


0)
=

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

2) 2) if (nume(mid] 0) 3) if(nums (mid) ==1)


(mid)
==

(nums
==
1) If
his swap (mid, lot midt+;
swap (mid,
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
into
it's t
R
Ques : Merge two sorted arrays .
int i=
m-1

~
518
arrs.
inti
intk
n-
=

mth-1=
1

warz s1zYTE
j

merars,
is
it to
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,

1, 2, 3 1, 3, 22, 1, 3 2, 3, 1 3,1,2 3,2, 1


idx

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 EU 3 for (inti n-2;


=
ix,0;i --
1

if (arr[i] < arr (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

i, j - reverse (nume. begin))+ i, nums, begin () + j+ 1);


Ques : Find the next permutations of Array .
Note :- If not possible then print the sorted order in
ascending order. idx 0
=

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
i< 12; it+ 12
for lint:= 1;
arr (i):
max;

if(max< heights (i)) max=


heights (i];
3
Next Greatest Element
A way:
&

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;

if (max <heights (i]) max=


heights [i];
3
Trapping Rain Water…continued
he 010 21013212 1

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

You might also like