You are on page 1of 20

Today's agenda

U missing Positive
first
majority element
majority element
2

went greater element 3


leetcode41 First missing Positive Amazon Microsoft Apple
Given an la find first missing natural number
II 3.4.5.7
En as 53 3 2 I 2 7 4

an 7 8 2 6 4 7 I 3 35

as 6 2 I 6 4 3 5 7

as 43 i 2 I 4 3 5

as 43 2 3 8 7 I

flidead
4 Sort the
array
as 53 3 2 I 2 7

I
2 A 2 3 7 4
7 c
ocaloga to a ocaloga
I ideal

Obst and I 2 3 I I t I

man
rigs ans

d 8

assai I I 3 a

ios Nt I
I

ans
range 41 att

0552 answer range LI att

LI N Its

aorta I I I t ee
En ask 4o I
e
s i e
g j 8
answer
range 11,93

myanswersange 41,83
0 I 2 3 4 5 6 7

I I 3 I 15 6 I 8

How to do this mapping Swapping


1.83
T o e r s y s e n

Asolo 4 2 71 6 9 16 8 31
3 4
I ans S

asf 4 inden 3 Swap


0,3
Asolo 6 inden 5 swap 0,5
asolo I inden O increment
an 113 2 index I increment
982123 7 irrelevant increment
an 3 4 inden 3 increment i
as 47 9 irrelevant increment

ask 3 inden 2 Swap m2


I PSuedo code
LI Htt Lane

int missing
Positive
Integer int anti L
int i O

while i f
É
a

if Catania
itt

else f
int id n aioli I
arolidn in
bland
Fc n else I swap i idn 3
g
S C old 3

i antis a return it
if
g
return at 19

21,5
edge case an 153 I j 23

21,83
j j I j jT
int missingPositiveInteger
int i o
int area I hassle
I
y4 g
6

while C a I th ans 5
ingrain.meiiI
3 man no Swapping N
m
int idn aioli e
swap i ida
21,5

y cagecase a so
idn 2
int i icon itt
for O

if Ci4 and
8
a return it

return att

y
leeteode 1659 majority Element h Amazon Apple
Given as far find majority element
An ele with freq Mz
majorityelement is definitelyPresent

End as 16 I 2 I 6 1 13 ans I
4 612 3 occ

En2 as 9 3 4 4 8 4 9 4 3 43 ang 4
4912 74 one

En3 arr 1123 3 3 4 6 1 3 25 3 3 33


612 2 76 Occ U an 3

En 4 as Io I 4 6 5 3 4 54 4 4 83
10 2 750
invalidinput Ls no majority
element
note majority element is Present in the
assay

I ideal
use nested loop n Tc o n

I idea 2
Sort the Tc
assay ocaloga

idea
use hashnnap to count e c OCD s c O Cd
Hidea4 expected 7 c Ocn S C 0 a
UBoyer moore voting algo
05518
4 How
many majority elements can be
Present in
one
array Atman I

freq maji
a 2

42
freq maja
In total you
sa N
I t say have only
elements in
the array

77
Comb
0552 15 People to Participate

Subhash Fx Fx 8

Nishant FXEX

Abhishek 81 9
Comb2 77

15 People to Participate

Subhash EX 4998k

Nishant all 1 EX

Abhishek ax El El El
Y o ca

arrest L 38 go 3

if we cancel out 1
majority element with
no

1 non majority element number the non cancelled


number at the end is going to be majorityelem
0353

you don't need to know


majority element

you cancel
out
3ggtg IImen

Summary Cancel out 2 distinct elements one by


one the uncancelled element is
going to
be majority element

I I I 3 t t t t s s o

En as it I 3 3 4 6 1 3 2 5 3 3 33

é ele
freq
I 0

I
3
3
1

2 ans 3
2 3 I

3 3 O

4 I 1
5 1 O
6 2 1
7 2 O
8 3 1
9 3 2
g
to 3 3

11PSuedo Code

P S int majorityelement int ash

int val are 63


int count I

int it icon itt


for val h
7 c OCA
if Caroli
S C OC count t

3
else
count oh
if val anti
Out
Eye
Count
3 3
3
return Val
3
a majority element 2
Given Integer array of size n find all elements
an

that appear more than CBI times


Unot necessarily majority element is Present

En I s i i i 3 5 33 I 12.33

an i s i's i's i's's ii a

11 Boyer moore voting idea

Obst
22
total length in

I 73 of
n Xx
En i's i i i

A A rest
may't Maja

3 elements at once
Cancelling distinct
create distinct triplets

En

24 2 11

I
counts

Val2 23 42 3,43 12,3 43


0 121
2,3 53
a 1
C
Assaylist Integer Is new
Array list
List Integer Is new Array list a c
11Psuedo code

List Integers majorityelement into nuns 4


List
integer ans new Array Liste c

int walk numso


int count I 1
int rat 2 I 10
int count2 05
Tc Ola
int i is numb length itt h
s c Ola for numsci val 1 1
if Count Itt

else numsfi val 27


ifCount2 At

else count 1 1
if o

Valk nuns i
Count1 1

else count2 079


if
val2 nuns fi
Count2
19
else
count I g
Count2 3
g
int CI o

Lint i ic itt h
for
o n
Chameli I
if
val 1
catty

if Cct M3 I ans add rale

int c2 0

Lint i ic itt h
for
o n
Chameli valid I Cantt
if

73 ans add Crazy


if C2

return ans

wins
int rata nunsso
intcounta I an i i
int raz to
intcounts o

i nunslength itt h
for int inumscis val 1 2
if van

else
if numssis van
att
Count
µ I 2,3 5
else count1
if 071 Count2 0 10 O
3
Tune 1,43
else
if count2 071
i3
3E.I.gs

9
counta
g
on
g
a went greater element 3
Given a character with
digitsof a number
array
at indeness Find the Smallest integer which has exactly
the same digit and the value is greater than number inches

En I 2 7 21

En 9 7 5 3 2 f a

I idea int n

iterate from ntt till the answer


you get
check
for every integer if occase Same
or not

I idea2 Cho I 4 8 4 9 7 5 32

En I 4 8 49 7 5 32

2 4 7 I I 3
As we want to do
a minimum increment
24721 3 7 24 7 1 93
Problem
mindoo mongo according to
statement we target
Smaller Place value
first using swapping
Cho I 4 8 4 9 7 5 3 2

pm I 4 8 1 575 7
g g g 7 4 32
I
y
1
894 7532 1487945322

estPossible

4 8

51
either sort
1 4 85
ID seretse as

digitswere in
the
des
order
I PSuedo code

public int hentgreaterElement int n

Char I number Cnt toCharArrayC


int ion 1
Lint i number length 2 i i h
for o

number i c number ite h


if
idn i
break
s

if Cida 171
return 1

old char val numberIdn


7 c OCnlogn int smallest ion i data

s C 011
int ie i dnt i ie numberlength itt l
for number i val Id number file
if numbersmallestids

Smallest ida is

3
char temp numberlidn
number idol numberEsmallestion

number smallestida temp


Arrays Sort number i dnt 1 number length

long ans LongparseLong new string numberD

h
if ans Integer MAX
return 1
VALUE

else 3
return int ans

I
o I 2 3 4 5 6 7
mom 83
Lint i numberlength 2 i o i h
for number fi c number ite h 2 3 4 75
if i
idn emanation u
break
g I I
I 4 8 5 23 479
return 1

char val number lids char temp numberlion

int smallestidn i data number ios numberEsmallestion


numbersmallestion temp

ie numberlength itt 9
for int ie idnti d

AwaysSort number idn 1 numberlength


Smallestida i
g

You might also like