You are on page 1of 14

Today's agenda

Backtracking Into
All numbers 2vasia

Subset Sum I variation


11Backtracking Into
4 Generating all Possibilities to get Cosect answer

maze Harry Potter


grit

iEÉ

Backtracking codes are written using Recursion


Q point all in
n digits numbers
formed by 1 and 2
increasing order
A 22 7 11
12 I 1 2
21 I 2 1

2 I 22
2 I 1

2 12
2 21

2 22

U How can have using 122


many M
digit number you
1424
EX I x I 2h

1 idea

refer bits class


of Advanced
I Backtracking idea
0 I l 3

1,2
arris I I t
o f
g 111

t as

I 2 2
1
21 2
Psued code
I I
void Pointall int arc inti int n h
Ci N
if is iterate Point the an
7c 012 m
return
S C OG 3

off'tOcd anti 1
soCN
d

anti 2
PointAll Caro itt ni

main 1 L
int arsine
Pointall am O N

int 2 10

É
pointed away

points are 4K
Steele
tract
Pointall int an c inti int n h
e
void

ifC is N I l
isiterate pointthe an
return

I
am i I
are arr it I m
Point É
anti 2
a
g

I I 1
1 12
I 2
I 22
Q point all n digits numbers
formed by 1 2 3,4 5 in
increasing order
N 2 5 XI 25

void Pointall int an c inti int n h


C is N I l
if is iterate Point the an

setum Te off n a

5 CC O fa
anti 1
Pointare arr it I m

anti 2
PointAll Caro i ti ni
int vet ve S utt
for
PointAll Caro i ti

J
ni
Pointall and in nD

PointAll Caro i ti ni

anti 5
PointAll Caro i ti ni

g
Golden rule
of secession
or backtracking

no recessive calls All the options you have at that


of
spot

Break till 8 27 AM
Q Given a assay elements count no
of Subsets with sum k

an is i i i is tans

63 4 7 13 2 1 53

areas 15 2 73 I 5
t
7

i e i
ÉÉÉ
5 12
figin
É 5I
io e
s
has
11 PSuede Code
int count o

void subsets un int arolnd int k int i intSumy


if Ii if
n
sum a k count ft

return

Suntans
Sum
lily
D
S C Ola
reject in

I
Sum Sum anti element
Subsetsum far k itt Sam

main C C
subsetsun Can K O o
Count A
sissies
void intis inti intsum
if Ii a
infants
if sum ksecountto
are 15 I 73

Sum Suntans
lily is
i
Subset
sum far k its Sam send

g
a Point all subsets with Sum K

K
asses IÉ I 7

void subsets un int arolnd int k int i intsum lists ly


point d 3
if sum k

return
I iii

Suntans
Sum
l add anti lily
element

Sum Sum aeolid rejectith


losenove e Si ell
I
1 element
Subsetsum far k itt Sam l

3
Toeing

void subsetsun intarolnd int k inti intsum lists ly


i a
if if sum Kil Bint d 3
return J 2
Suntans
Sum
fig
sissifiedb is
selectmen
in same

sit.is stiiiiianies I

1523
i
Point all

I 23

void Pointall int an c inti int n h


E
if Ci iterate
is N
pointthe an is
setum

Pointan laser itt n

PointAllCaro i ti ni

I t
I 1 2

You might also like