Lec 1
Lec 1
Sessions
10ᵗʰ Mon 7PM onwards coding
11ᵗʰ Tue 11
Wed
12ᵗʰ
Session
Aptitude
13ᵗʰ Thu
Pattern
Problem statement
1
Taller
3 Approach own
on your
4 Code
10kt
4 Dreamy Dcentist
Gatecs
Jain Sir PW
telegram Aditya
f
code here
Share your
t.me AdityaSir PW
[NAT]
#Q. Sort Colors
Problem Statement : Given an array nums with n objects colored red, white,
or blue, sort them in-place so that objects of the same color are adjacent,
with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and
blue, respectively.
You must solve this problem without using the library's sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2] Red
Example 2:
I
Input: nums = [2,0,1]
Output: [0,1,2]
white
Constraints:
• n == nums.length Blue 2
• 1 <= n <= 300
• nums[i] is either 0, 1, or 2
001121
API Sfg 2
1,0 1,012
in order
Sort Is
0,0 11,22
Best WI Olde
Apr
1 Two pointer
o n t
if int n a sized
elem a
for 0
iF elem
itt
elnif dem 1
j
i 0s
j T
to i
i
j
k
2 s
for is
4
3
[NAT]
#Q. Remove duplicates from Sorted Array
Problem Statement: Given a sorted array arr[] of size n, the goal is to
rearrange the array so that all distinct elements appear at the beginning in
sorted order. Additionally, return the length of this distinct sorted subarray.
Note: The elements after the distinct ones can be in any order and hold any
E
11112
Example 1:
Input: arr[] = [2, 2, 2, 2, 2]
Output: [2]
Explanation: All the elements are 2, so only one instance of 2 is kept.
Example 2:
Input: arr[] = [1, 2, 2, 3, 4, 4, 4, 5, 5]
Output: [1, 2, 3, 4, 5]
Explanation: All distinct elements are extracted and placed at the beginning
in sorted order.
Approach Map Set Auxilary
Spare
Anod Additional
1711313 Space
GUV
1 0
ali i i
12 if
as ali
1
3 t
3
Example 3:
Input: arr[] = [1, 2, 3]
Output: [1, 2, 3]
Explanation: No change is required as all elements are already distinct.
[MCQ] v
#Q.
V.INT
Leaders in an array
Problem Statement : Given an array arr[] of size n, the task is to find all the
Leaders in the array. An element is a Leader if it is greater than or equal to
0
all the elements to its right side.
Note: The rightmost element is always a leader.
Examples The
Example 1:
Aditya Jain Sir P
Input: arr[] = [16, 17, 4, 3, 5, 2] Telegan
Output: [17, 5, 2]
Explanation:
• 17 is greater than all the elements to its right [4, 3, 5, 2], so it is a leader.
• 5 is greater than all the elements to its right [2], so it is a leader.
• 2 has no elements to its right, so it is a leader.
A 16117,43,5121
Yte
4
Flag 11
i n
for
o
for j Pti n
i
if arj a
I flag 0
break
if Flag 1
Is point at i
Example 2:
Input: arr[] = [1, 2, 3, 4, 5, 2]
Output: [5, 2]
Explanation:
• 5 is greater than all the elements to its right [2], so it is a leader.
• 2 has no elements to its right, so it is a leader.
Appry Optimised
or equal to
than
elem greater
AJ
fan
the elements on its sight
µ
than or email to
an elem queater
lem on its sight
the
L R
L RT
maxenjan
elus
areola
max
Lead
elegant
max
[NAT]
#Q. Missing and Repeating in an Array
Problem Statement : Given an unsorted array of size n, where the array
elements are in the range 1 to n, one number from the set {1, 2, …, n} is
missing, and one number occurs twice. The Fy
task is to find these two
numbers. Examples
Example 1:
Input: arr[] = {3, 1, 3}
Output: 3, 2 320
Explanation: N
• 3 occurs twice in the array.
1 3 I
• 2 is missing from the array.
Example 2:
Input: arr[] = {4, 3, 6, 2, 1, 1}
Output: 1, 5
T missing
Explanation: 1 elum
• 1 occurs twice in the array. twice
• 5 is missing from the array. I chu
API Sort
1 3 3
it
if ali
It
missing_elem
3
i a iti
PF a
a i
duplicate
3
[NAT]
1BI5
#Q. Stock Buy and Sell – Max one Transaction Allowed
Problem Statement: Given an array prices[] of length N, representing the
prices of stocks on different days, the task is to find the maximum profit
o
possible by buying and selling the stocks on different days when at most
one transaction is allowed.
Here, one transaction means one buy and one sell.
Note: The stock must be bought before being sold.
Is
Examples
Example 1: T.IE
I
17
sell
buy
8171311 3 buy
sell
20
MI Max array
Mr
min array
maD
an
min
Buy sell
API for buying
on
ith
day
profy j.it
selling on
turn
so.to
min
selling
off
buy
all
y date
buy
min elen 9101
0
profit
n
i i
For
profit
max profit du
a i min
3
Explanation:
• Since the array is sorted in decreasing order, no profit can be made.
Example 3:
Input: prices[] = {1, 3, 6, 9, 11}
Output: 10
Explanation:
• The array is sorted in increasing order, so we can make the maximum
profit by buying at 1 and selling at 11, yielding a profit of 11 - 1 = 10.
[MCQ]
#Q. HI
Stock Buy and Sell – Multiple Transaction Allowed wife
Problem Statement: Given an array prices[] of size n denoting the cost of
stock on each day, the task is to find the maximum total profit if we can buy
and sell the stocks any number of times.
Constraints:
• We can only sell a stock that we have previously bought.
• We cannot hold multiple stocks at the same time (i.e., we must sell before
buying again).
Examples
Example 1:
T NGE
Input: prices[] = {100, 180, 260, 310, 40, 535, 695}
Output: 865
Explanation:
• Buy on Day 0 and Sell on Day 3 → 310 - 100 = 210 is