0% found this document useful (0 votes)
4 views574 pages

Phitron Problem Solving Notes Basic Data Structures

Uploaded by

ahmedragibhasan8
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)
4 views574 pages

Phitron Problem Solving Notes Basic Data Structures

Uploaded by

ahmedragibhasan8
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

AI/ML Track

Python Documentation
Python Docs
Installation Files
Python Installation Guideline​

Python : [Link]

VS Code: [Link]

Required Extensions for VS Code:

1.​ Python
2.​ Jupyter

Required Packages for Python:

1.​ Jupyter
2.​ Ipykernel
Temp1
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
long long n;

cin >> n;

vector<long long> a(n);

long long int mx = 0;


// long long int mx_2 = 0;

for (long long int i = 0; i < n; i++)


{

cin >> a[i];

mx = a[0] + a[n-1];
}

// sort([Link](),[Link]());

long long int l = 0, r = 0;

long long sum = 0;

for (long long i = 0; i < n-1; i++)


{
// if (mx_1 == a[i])
// {
// if (i == n - 1)
// {
// l = a[i - 1];
// r = a[0];
// }
// else if (i == 0)
// {
// l = a[n - 1];
// r = a[i + 1];
// }
// else
// {
// l = a[i - 1];
// r = a[i + 1];
// }

// mx_2 = max(r, l);


// }

long long temp = a[i]+a[i+1];

if(temp > sum)


{
sum = temp;
}

cout<<max(mx,sum)<<"\n";

return 0;
}
💥 Problem Solving Club
AtCoder Tags

Lecture 1

[Link]

Git Repo of Leetcode Problems - [Link]

Git Repo of building projects from scratch - [Link]

ios_base::sync_with_stdio(false);
[Link](NULL);

if (!(cin >> n >> s)) return 0;


OP on Arr Prblms
[Link]
😏 String Problems
Usually found in CodeChef Contest Div-4 Problem C

START 200

[Link]

Rating-CC-1133

Solve 1

#include <bits/stdc++.h>
using namespace std;

int main() {
​ int t;
​ cin>>t;

​ while(t--){
​ int n;
​ cin>>n;

​ int len = n;

​ string s = "abc", res;
​ while(n--){
​ res += s;
​ }

​ cout<<[Link](0, len)<<endl;
​ }

}
✅ 1. What does f(S) mean?
In math and in programming problems,​
f( ) is just a function notation.

●​ If you see f(x), it means “apply function f on value x”.​

●​ Here, the function takes a string instead of a number.​

So:

👉 f(S) = the number of distinct palindromic substrings of S


It is not an equation you solve.​
It is simply a definition.

Examples:

●​ If S = "abba", then the palindromic substrings are:​

○​ "a", "b", "bb", "abba"​

○​ So f("abba") = 4​

●​ If S = "aaaa", palindromes are:​

○​ "a", "aa", "aaa", "aaaa"​

○​ So f("aaaa") = 4 (distinct ones)​

That’s all f(S) means.

✅ 2. What does the problem want?


A string S of length N, made of lowercase letters, such that:
➤ f(S) ≤ 5

(i.e., at most 5 distinct palindromic substrings)

This means the string must not contain too many unique palindromes.

❗ Key Observation
If we pick a string like "aaaaa...", it creates HUGE numbers of palindromes.

But strings with repeating patterns of non-symmetric characters, like:

abcabcabcabc...

…produce very few palindromes, because:

●​ "a", "b", "c" → palindromes​

●​ No two-letter palindrome like "aa" exists​

●​ No three-letter palindrome like "aba" exists​

So f(S) = 3 basically.

That’s why this trick works.

✅ 3. Explanation of the given solution


string s = "abc", res;
while(n--){
res += s;
}
cout << [Link](0, len) << endl;

Breakdown:

Step 1 — Base pattern


"abc"
This pattern has only 3 palindromic substrings:

"a", "b", "c"

Step 2 — Repeat it to reach length N

If N = 10, then repeating “abc” gives:

abcabcabcabcabc...

Step 3 — Cut the first N characters

[Link](0, len) just extracts exactly length N.

🎯 Why does this guarantee f(S) ≤ 5 ?


Because repeating "abc" never creates new palindromes:

●​ No "aa", "bb", "cc"​

●​ No "aba" or "cbc" because characters don’t line up symmetrically​

●​ No long palindrome is possible in "abcabcabc..."​

So the only palindromes are still:

"a", "b", "c"

Which is 3 ≤ 5, so it satisfies the requirement.

🎉 Final Summary
●​ f(S) = number of distinct palindromic substrings.​
●​ Want f(S) ≤ 5.​

●​ "abcabcabc..." guarantees very few palindromes.​

●​ The solution prints the first N characters of "abc" repeated.


🤤 GCD Problems
Week 06

[Link]

Solve : Brute Force(WrongAnswer:Time Out)

#include <bits/stdc++.h>
using namespace std;

// naive/brute force approach

int main()
{

ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

int g = 0;

for (int i = 1; i <= n; i++)


{
for (int j = i + 1; j <= n; j++)
{
g = max(g, __gcd(i, j));
}
}

cout << g << "\n";


}
return 0;
}

Solve

#include <bits/stdc++.h>
using namespace std;

int main()
{

ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

cout << n/2 << "\n";


}
return 0;
}

You are given a number n.

From numbers:

1, 2, 3, ..., n

You must choose two different numbers a and b (a < b)​


and maximize:

gcd(a, b)

Step 1️⃣ Try small n like a human


Case: n = 3

Pairs:

●​ (1,2) → gcd = 1​

●​ (1,3) → gcd = 1​

●​ (2,3) → gcd = 1​

✅ Maximum = 1

Case: n = 4

Pairs:
●​ (2,4) → gcd = 2 ✅​
●​ (3,4) → gcd = 1​

●​ (1,4) → gcd = 1​

✅ Maximum = 2

Case: n = 5

Pairs:

●​ (2,4) → gcd = 2​

●​ (3,6) ❌ not allowed (6 > 5)​


●​ (4,5) → gcd = 1​

✅ Maximum = 2

Case: n = 6

Pairs:

●​ (3,6) → gcd = 3 ✅​
●​ (2,4) → gcd = 2​

●​ (4,6) → gcd = 2​

✅ Maximum = 3

Case: n = 7

Pairs:
●​ (3,6) → gcd = 3​

●​ (4,6) → gcd = 2​

●​ (5,7) → gcd = 1​

✅ Maximum = 3

Case: n = 8

Pairs:

●​ (4,8) → gcd = 4 ✅​
●​ (3,6) → gcd = 3​

●​ (2,6) → gcd = 2​

✅ Maximum = 4

Step 2️⃣ Pattern (human discovery)


Let’s write results:

n answe
r

2 1

3 1

4 2

5 2

6 3

7 3
8 4

9 4

10 5

💡 Answer is always n ÷ 2 (integer division)

Step 3️⃣ WHY this happens (with numbers)


Let’s say the gcd is g.

That means:

●​ both numbers are divisible by g​

so the numbers look like:​



g × something

●​

The smallest pair with gcd = g is:

(g, 2g)

Example:

●​ gcd = 3 → (3,6)​

●​ gcd = 4 → (4,8)​

●​ gcd = 5 → (5,10)​

Now the key restriction:

both numbers must be ≤ n


So:

2g ≤ n

Which means:

g ≤ n / 2

Step 4️⃣ Can we actually reach that value?


Yes.

Take:

g = floor(n / 2)

Then choose:

a = g
b = 2g

Example:

●​ n = 7 → g = 3 → (3,6)​

●​ n = 10 → g = 5 → (5,10)​

Both are ≤ n, and:

gcd(g, 2g) = g

So this is achievable, not just theoretical.


Step 5️⃣ Why nothing larger is possible
Try to beat n/2.

Example: n = 10

●​ Try gcd = 6​

●​ Smallest pair = (6,12) ❌ 12 > 10​


Impossible.

So no gcd larger than n/2 can exist.

🧠 Human conclusion
●​ The best gcd always comes from a number and its double​

The biggest possible double that still fits in [1..n] is:​



(n/2, n)

●​

So the answer is simply:​



floor(n / 2)

●​

Step 6️⃣ Final answer (no tricks)


For each test case:

print n / 2
[Link]

Solve

#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);
int t;
cin >> t;

while (t--)
{
int n;

cin >> n;
// this block of code does the magic of storing the last index of
the array elements ,here the values are used as indices and their indices
are stored in their value location

//************************************ */

vector<int> idx(1001, -1);

for (int i = 1; i <= n; i++)


{
int x;
cin >> x;

idx[x] = i;
}

//*********************************** */

int mx = 0;

for (int x = 1; x <= 1000; x++)


{
if(idx[x] == -1)
{
continue;
}

for (int y = 1; y <= 1000; y++)


{

if (idx[y] == -1)
{
continue;
}

if(__gcd(x,y) == 1)
{

// cout<<"current max = "<<mx<<"\n";


mx = max(mx, idx[x] + idx[y]);

// cout<<"show me i & j "<<i+1<<" "<<j+1<<" i+j=


"<<i+j+2<<" GCD- "<<__gcd(a[i],a[j])<<"\n";
}
}
}

if (mx == 0)
{
cout << -1 << "\n";
}
else
{
cout << mx << "\n";
}
}
return 0;
}

We are given:

●​ an array a of length n​

●​ each value is between 1 and 1000​

●​ we must find two positions i and j (they can be the


same)​

●​ such that a[i] and a[j] are coprime​


●​ maximize i + j​

If impossible → print -1.

Step 1️⃣ Think like a human (try small examples)

Example:

[1, 3, 5, 2, 4, 7, 7]

index:1 2 3 4 5 6 7

Let’s try to maximize i + j, so naturally we want large


indices.

Check from the right:

●​ index 7 → value = 7​

●​ index 6 → value = 7​

●​ index 5 → value = 4​

Are 7 and 4 coprime?​


👉 gcd(7,4) = 1 ✅
So answer = 7 + 5 = 12

💡 Observation:​
We don’t care about all indices — we care about the
largest index where a value appears.

Step 2️⃣ Key constraint that changes everything

1 ≤ ai ≤ 1000

This is HUGE.

That means:

●​ At most 1000 distinct values​

●​ But n can be 200,000​

So instead of thinking in terms of indices, we think in


terms of values.

Step 3️⃣ Human simplification

Instead of checking all (i, j) pairs (impossible), we do


this:
For each number from 1 to 1000:

👉 remember the largest index where it appears


Example:

Array: [1,3,5,2,4,7,7]

Value → largest index

1 → 1

2 → 4

3 → 2

4 → 5

5 → 3

7 → 7

Step 4️⃣ Now think in values, not indices

We now try all pairs of values (x, y) such that:


●​ gcd(x, y) = 1​

●​ both values exist in the array​

For each such pair:

candidate = lastIndex[x] + lastIndex[y]

Take the maximum.

Step 5️⃣ Dry run with real numbers

Example:

Values present: {1,2,3,4,5,7}

Try some pairs:

x y g indi su
c ces m
d

7 4 1 7+ 12
5

7 2 1 7+ 11
4

5 4 1 3+ 8
5

3 2 1 2+ 6
4

Maximum = 12

Step 6️⃣ Why this is fast enough

●​ Values range only from 1 → 1000​

●​ Total value pairs = 1000 × 1000 = 1,000,000​

●​ GCD is very fast​

●​ Test cases ≤ 10​


This easily runs within limits.

Step 7️⃣ Edge case (important)

You are allowed:

i = j

So if a value is:

●​ coprime with itself (only true for 1)​

●​ and appears at index k​

Then:

answer = k + k = 2k

This is why 1 is very powerful.


Week 6 - M

[Link]

Subset GCD problem Solution | Starters 118 | Explanation + Live Coding (C++)

#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;

cin>>t;

while (t--)
{
int n,k;

cin>>n>>k;

int a = n/k;
// cout<<a<<"\n";

for (int i = 1; i <= k; i++)


{

cout<<i*a<<" ";

}
cout<<"\n";

}
return 0;
}
🤕 LCM Problems
Week-06

Basic LCM Function

#include <bits/stdc++.h>
using namespace std;

int LCM(int a, int b)


{
// return (a*b)/__gcd(a,b) ;

// to avoid overflow technique

return (a / __gcd(a, b)) * b;


}

int main()
{
int a, b;
cin >> a >> b;

cout << LCM(a, b) << "\n";


return 0;
}
Problem - 1389A - Codeforces

Solve 1

#include <bits/stdc++.h>
using namespace std;

// CF-*800

int LCM(int a, int b)


{

return (a / __gcd(a, b)) * b;


}

int main()
{
int t;

cin >> t;

while (t--)
{
long long l, r;

cin >> l >> r;

long long x = l;

long long y = l * 2;

if (y <= r)
{
cout << x << " " << y << "\n";
}
else
{
cout << -1 << " " << -1 << "\n";
}
}
return 0;
}

Solve 2

#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--) {
long long l, r;
cin >> l >> r;

// If the smallest multiple of l is out of range


if (2 * l > r) {
cout << "-1 -1\n";
} else {
// x divides y, and LCM(x, y) = y
long long x = l;
long long y = 2 * l;
cout << x << " " << y << "\n";
}
}

return 0;
}
Problem in plain words
You’re given a range [l, r].​
You must pick two different numbers inside this range such that:

●​ their LCM is also inside the same range​

🔎 Step 1: Try random numbers (human


instinct)
Suppose:

l = 10, r = 40

Let’s try some pairs:

x y LCM(x,y)

10 15 30

10 18 90 ❌
12 18 36

15 20 60 ❌
You’ll notice something:

●​ Sometimes LCM is too big​

●​ Sometimes it stays inside​


Why?

🔎 Step 2: Observe a pattern


Look at successful cases:

x y divides? LCM

10 20 10 divides 20 20

12 24 12 divides 24 24

7 14 7 divides 14 14

💡 Whenever x divides y, the LCM equals y​


That’s simple and predictable.

🔎 Step 3: What if x does NOT divide y?


Example:

x = 6, y = 10

●​ 6 does NOT divide 10​

●​ LCM(6,10) = 30​

Now instead of using (6, 10), we use:

(6, 30)

Check:

●​ 6 divides 30 ✅​
●​ LCM(6, 30) = 30​

●​ If 30 is inside [l, r], then this pair is better​

👉 This is what the editorial means by:


“We could have chosen x and LCM(x, y) instead”

🔎 Step 4: Why the editorial says “assume


x divides y”
Because:

●​ If x doesn’t divide y → replace y with LCM(x,y)​

●​ After replacement → x does divide the new y​

So every solution can be converted into one where:

x divides y

No imagination needed — we literally replace the number.

🔎 Step 5: What’s the SMALLEST


multiple?
Now let’s say:

l = 7, r = 50

What are multiples of 7?


7, 14, 21, 28, 35, 42, 49

The smallest multiple larger than 7 is:

14 = 2 × 7

So the best chance is:

x = 7
y = 14
LCM = 14

🔎 Step 6: When does it fail?


Suppose:

l = 30, r = 50

●​ Smallest multiple of 30 is:​

2 × 30 = 60 ❌ (outside range)
Other multiples?

90, 120, ...

All worse ❌
So no solution exists.
🧠 NOW translate back to symbols (after
understanding)
Human idea Math version

x divides y `x

Smallest multiple 2x

LCM becomes simple LCM(x, y)


= y

Check if possible 2l ≤ r

✅ Final editorial logic (human version)


1.​ Any valid solution can be changed so that the first number divides the second.​

2.​ The smallest such pair is (l, 2l).​

3.​ If 2l fits in [l, r], answer exists.​

4.​ Otherwise, it’s impossible.​

🧪 One last dry run


Input:
l = 13, r = 25

●​ 2 × 13 = 26 ❌​
●​ No valid pair​
Input:
l = 13, r = 30

●​ 2 × 13 = 26 ✅​
●​ Output: 13 26
😬 Tow Pointer Problems
C. Prepend and Append

Solve 1

#include <bits/stdc++.h>
using namespace std;

// [Link]

//CF rating 800

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

string s;
cin >> s;

int l = 0, r = n - 1;
bool flag = true;

while (l <= r)
{
if (s[l] == '1' && s[r] == '1' || s[l] == '0' && s[r] == '0')
{
flag = false;
break;
}
else
{
l++;
r--;
}
}

// cout << "size of present L : " << l << "\n";

int ans = 0;

if (!flag)
{
ans = n - (l*2);
// cout << "size of the given string was : " << n << "\n";
cout << ans << "\n";
}
else
{
// cout << "size of the given string was : " << n << "\n";

cout << 0 << "\n";


}
}
return 0;
}

Solve 2
B. Number of Smaller

Sum of Two Values


2

A. Segment with Small Sum

B. Segment with Big Sum


😬 Sliding Window Problems
CSES - Playlist

Distinct Values Subarrays

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
[Link](NULL);
int n;
cin>>n;
map<int,int> last_index;
long long int ans = 0;
for (int j = 1, i = 1; j <= n; j++)
{
int x;
cin>> x;
i = max(i,last_index[x] + 1);
last_index[x] = j;

ans += (ans, j - i + 1);


}

cout<<ans<<"\n";

return 0;
}
Distinct Values Subarrays II
Variable Size Sliding Window
[Link]

Brute force with TLE

#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(false);
[Link](nullptr);

long long n, s;
if (!(cin >> n >> s)) return 0;
vector<long long> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];

long long best = 0;

// Brute force: try every starting index l and every ending index r >=
l
for (int l = 0; l < n; ++l) {
long long sum = 0;
for (int r = l; r < n; ++r) {
sum += a[r];
if (sum <= s) {
long long len = r - l + 1;
if (len > best) best = len;
}
// optional early break: if sum already > s and all a[i] are
positive,
// further extending r will only increase sum, so we could
break.
// But since this is a brute-force demonstration, leaving it
is fine.
// if (sum > s) break;
}
}

cout << best << '\n';


return 0;
}

Dry Run

Perfect 👍 Let’s dry-run the brute-force code step-by-step on the sample input:
n = 7, s = 20
a = [2, 6, 4, 3, 6, 8, 9]

Goal

We want the longest segment [l..r] (1-based) such that​


a[l] + a[l+1] + ... + a[r] <= 20.

🧩 Outer loop: l = 0
We start from index 0 (a[0] = 2).

r Elements in segment Sum <= 20? Length bes


a[l..r] t

0 [2] 2 ✅ yes 1 1

1 [2,6] 8 ✅ yes 2 2

2 [2,6,4] 12 ✅ yes 3 3

3 [2,6,4,3] 15 ✅ yes 4 4

4 [2,6,4,3,6] 21 ❌ no - 4
5 [2,6,4,3,6,8] >21 ❌ - 4

6 [2,6,4,3,6,8,9] >21 ❌ - 4

→ For l=0, the best segment ends at r=3 (sum = 15, len = 4).

🧩 Outer loop: l = 1
We start from index 1 (a[1] = 6).

r Segment Sum <= 20? Length bes


t

1 [6] 6 ✅ 1 4

2 [6,4] 10 ✅ 2 4

3 [6,4,3] 13 ✅ 3 4

4 [6,4,3,6] 19 ✅ 4 4

5 [6,4,3,6,8] 27 ❌ - 4

Still max length = 4.

🧩 Outer loop: l = 2
Start at a[2] = 4.

r Segmen Sum <=20? Length bes


t t

2 [4] 4 ✅ 1 4

3 [4,3] 7 ✅ 2 4

4 [4,3,6] 13 ✅ 3 4

5 [4,3,6,8] 21 ❌ - 4
Still best = 4.

🧩 Outer loop: l = 3
Start at a[3] = 3.

r Segmen Sum <=20? Length bes


t t

3 [3] 3 ✅ 1 4

4 [3,6] 9 ✅ 2 4

5 [3,6,8] 17 ✅ 3 4

6 [3,6,8,9] 26 ❌ - 4

Still best = 4.

🧩 Outer loop: l = 4
r Segmen Sum <=20? Length bes
t t

4 [6] 6 ✅ 1 4

5 [6,8] 14 ✅ 2 4

6 [6,8,9] 23 ❌ - 4

🧩 Outer loop: l = 5
r Segmen Sum <=20? Length bes
t t

5 [8] 8 ✅ 1 4

6 [8,9] 17 ✅ 2 4
🧩 Outer loop: l = 6
r Segmen Sum <=20? Length bes
t t

6 [9] 9 ✅ 1 4

✅ Final Answer: 4

💡 Explanation
The longest “good” segment is any of these (all length = 4):

●​ [2, 6, 4, 3]​

●​ [6, 4, 3, 6]​

Both have sums ≤ 20.


Longest Substring with K Uniques | Practice | GeeksforGeeks

int n = [Link]();
int l = 0, r = 0, ans = -1;

map<char,int> cnt;

while(r<n)
{
cnt[s[r]]++;
if([Link]() == k)
{
ans = max(ans,r-l+1);
}
else
{
while([Link]() > k && l <= r)
{
cnt[s[l]]--;
if(cnt[s[l]] == 0)
{
[Link](s[l]);
}
l++;

}
}
r++;
}

return ans;
🙏 Contests-Upsolve
Speed Contest 4
C

[Link]

[Link]

Solve 1

#include <bits/stdc++.h>
using namespace std;

void solve() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
int one=0,zero=0;
for (int i = 0; i < n; i++)
{
one+=arr[i]==1;
zero+=arr[i]==0;
}
cout << (zero%2==0 ? "YES" : "NO") << "\n";

int main() {
ios::sync_with_stdio(false);
[Link](NULL);

int t; cin >> t;


while (t--) {
solve();
}

return 0;
}
Solve 2

#include<bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](NULL);

int t; cin >> t;


while (t--)
{
int n; cin >> n;
vector<int> b(n);
int sum = 0;
for(int i = 0; i < n; i++)
{
cin >> b[i];
sum += b[i];

if(sum % 2 == n % 2) cout << "YES\n";


else cout << "NO\n";
}

return 0;
}
CodeShef
Contests Page

Coding contests & Challenges

STAT 100 DIV-4

[Link]

START 200

[Link]

Rating-CC-1133

Solve 1

#include <bits/stdc++.h>
using namespace std;

int main() {
​ int t;
​ cin>>t;

​ while(t--){
​ int n;
​ cin>>n;

​ int len = n;

​ string s = "abc", res;
​ while(n--){
​ res += s;
​ }

​ cout<<[Link](0, len)<<endl;
​ }

Explanation

✅ Why repeating "abc" gives a good


string (f(S) ≤ 5)
Key idea:

If you repeat a short, non-palindromic pattern like "abcabcabc...",​


then the string never forms many distinct palindromic substrings.

✔ Step 1 — What palindromes exist in "abcabcabc..."?


Take the infinite string:

abcabcabcabc...

Look for palindromic substrings.

Only palindromes possible:


●​ "a"​

●​ "b"​

●​ "c"​

And no longer palindrome exists, because:

●​ "ab" ≠ "ba" → not a palindrome​

●​ "bc" ≠ "cb" → not a palindrome​

●​ "ca" ≠ "ac" → not a palindrome​

●​ "abc" reversed = "cba" → not equal​

●​ Any longer substring crosses boundaries where symmetry breaks.​

So the number of distinct palindromes is:

f(S) = { "a", "b", "c" } = 3

Which is ≤ 5.​
Therefore: ANY length N substring of "abcabcabc…" is good.

✔ Step 2 — How the code generates the string


string s = "abc", res;
while(n--){
res += s;
}

This repeats "abc" many times:

abcabcabcabc...
Then:

cout << [Link](0, len);

It prints exactly N characters of this repeated pattern.

Thus the final output is always:

abcabcabc... (first N characters)

This string always has only 3 distinct palindromic substrings → guaranteed ≤ 5.

⭐ Final summary (easy to remember)


●​ "abc" has no palindromes longer than length 1.​

●​ Repeating "abc" keeps it that way.​

●​ Any prefix of it (any length ≤ 100) still has only 3 palindromic substrings.​

●​ Since 3 ≤ 5, the string is always good.​

Solve 2
START207

[Link]

My Solve {1st Revision}

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

string s;

cin >> s;

// int l = 0 , r = n-1;

bool left_1 = false;

bool right_1 = false;

int cnt0 = 0;

int left_1_idx = -1;

int right_1_idx = -1;

for (int i = 0 ; i < n; i++)


{

if (left_1 == false && s[i] == '1')


{
left_1 = true;
left_1_idx = i;
}

}
for (int i = n-1; i >= 0; i--)
{
if (right_1 == false && s[i] == '1')
{
right_1 = true;
right_1_idx = i;
}
}

if (left_1_idx == -1)
{
cout << 0 << "\n";
continue;
}
else
{
for (int i = left_1_idx; i < right_1_idx; i++)
{
if (s[i] == '0')
{
cnt0++;
}
}

cout << cnt0 << "\n";


}
}

return 0;
}

[Link]
START208 Div-4

[Link]

#include <bits/stdc++.h>
using namespace std;

// START-208-Div-4-C

// Sabotage-CC-984

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
int n, x, k;
cin >> n >> x >> k;

vector<int> v(n);

for (int i = 0; i < n; i++)


{
cin >> v[i];
}

int r = 1;
sort([Link](), [Link]());

for (int i = 0; i < k; i++)


{
v[i] = 0;
x += 100;
}

for (int i = 0; i < n; i++)


{
if (v[i]>x)
{
r++;
}
}

cout << r << "\n";


}
return 0;
}
D

[Link]

#include <bits/stdc++.h>
using namespace std;

// START-208-Div-4-D

// GCND-CC-1477

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin>>t;

while(t--)
{

int n;
cin>>n;

vector<int> a(n);

set<int> st;

for (int i = 0; i < n; i++)


{
cin>>a[i];
[Link](a[i]);
}

if([Link]() == 1)
{
cout<<*([Link]()) - 1<<"\n";
}
else if([Link]() > 2)
{
cout<<*([Link]()) -1<<"\n";
}
else
{
int mx = *([Link]());
int mn = *([Link]());

for (int i = mx-1; i >=0; i--)


{
if(i != mn)
{
cout<<i<<"\n";
break;
}

return 0;

}
START 209

C. Small GCD

[Link]

#include <bits/stdc++.h>
using namespace std;

bool cmp(pair<int,int>&a, pair<int,int>&b)


{
if([Link] != [Link])
{
return [Link] > [Link]; // returning the higher score making it
position befor the lower numbers in the sort

}
return [Link] < [Link]; // when score is equal making the player
with the lower number position first in the sort
}
int main()
{
int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

vector<pair<int, int>> players;

for (int i = 1; i <= n; i++)


{

int score = __gcd(i, n);

players.push_back({score,i});

sort([Link](),[Link](), cmp); //this is a custom sort


where user dictates the rules of sorting instead of a default ascending
sort

for(auto ans : players)


{
cout<<[Link]<<" ";
}
cout<<"\n";

}
return 0;
}
[Link]

[Link]

START213

Div-4

[Link]

[Link]
#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin>>t;

while (t--)
{
int n,x;

cin>>n>>x;

vector<int> a(n);

for (int i = 0; i < n; i++)


{
cin>>a[i];
}

sort([Link](),[Link]());

int l = 0 ;

bool noflag = false;

while(l+1<n)
{
if(a[l] < x && a[l+1] > x || a[l] > x && a[l+1] < x )
{

noflag = true;
break;
}
l++;

if(noflag)
{
cout<<"No"<<"\n";
}
else
{
cout<<"Yes"<<"\n";

}
}

return 0;
}

START215

[Link]

D
START 218

[Link]

Solve

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);
int t;cin>>t;
while(t--)
{
int n;cin>>n;

vector<int> a(n+1),c(n+1);
for(int i=1;i<=n;i++)
{
cin>>a[i];

}
for(int i=1;i<=n;i++)
{
cin>>c[i];
}
ll sum=0;
int cnt=INT_MAX;

for(int i=1;i<=n;i++)
{
if(c[i]<cnt)
{
cnt=c[i];
}
sum+= (a[i]*cnt);

cout<<sum<<endl;

}
return 0;
}

Solve 2 : Same logic ,less lines, simpler

#include <bits/stdc++.h>
using namespace std;

int main() {
ios::sync_with_stdio(false);
[Link](NULL);

int T;
cin >> T;
while (T--) {
int N;
cin >> N;

vector<int> A(N), C(N);


for (int i = 0; i < N; i++) cin >> A[i];
for (int i = 0; i < N; i++) cin >> C[i];

long long ans = 0;


int minC = INT_MAX;

for (int i = 0; i < N; i++) {


minC = min(minC, C[i]);
ans += (long long)A[i] * minC;
}

cout << ans << "\n";


}
return 0;
}

Explanation

🧠 Restating the problem (in plain words)


●​ You have array A​

●​ You have array C​

●​ You delete elements from A one by one​

●​ When you delete the element currently at index i, you pay​


A[i] × C[i]​
●​ After deletion:​

○​ A gets reindexed​

○​ C stays fixed​

Goal: minimum total cost

🔑 Key observation (the heart of your


solution)
At any moment:

The cost multiplier you pay is the minimum C seen so far

Why?

Because:

●​ When you delete earlier positions, their indices shift left​

●​ So future deletions can reuse smaller C values​

●​ Once a small C appears, you can keep deleting elements using that same small cost​

So for position i:

●​ The best possible cost multiplier is​


min(C[1], C[2], ..., C[i])​

🧠 What your code is REALLY computing


This loop is the entire logic:
for(int i=1;i<=n;i++)
{
if(c[i]<cnt)
{
cnt=c[i];
}
sum+= (a[i]*cnt);
}

Let’s rewrite it in English:

“As I go from left to right, I keep track of the smallest cost seen so far,​
and I multiply the current A[i] by that smallest cost.”

That’s it.​
That’s the greedy strategy.

🧪 Dry run — Sample 1


Input
A = [4, 2]
C = [2, 1]

Step-by-step
i a[i] c[i] cnt = min so far cost
added

1 4 2 2 4×2=8

2 2 1 1 2×1=2

Total = 10 ✅
🧪 Dry run — Sample 2
Input
A = [4, 2]
C = [1, 2]

i a[i] c[i] cnt cost

1 4 1 1 4×1=4

2 2 2 1 2×1=2

Total = 6 ✅
Why does 2 get multiplied by 1?​
Because after deleting the first element, the remaining element shifts to index 1.

🧪 Dry run — Sample 3


A = [1,1,1,1,1]
C = [10,4,7,5,3]

i cnt adde
d

1 10 10

2 4 4

3 4 4

4 4 4

5 3 3

Total = 25 ✅
🧠 Why this greedy is ALWAYS optimal
Think in reverse:

●​ You want large A values to be multiplied by small C​

●​ Once a small C appears, it can be reused for all future deletions​

●​ So every element should be charged with the minimum cost available before it​

That is exactly what your loop enforces.

🧠 One-line intuition (memorize this)


Every element pays the cheapest cost that appears before it.

🎯 Final verdict
✅ Your solution is correct​
✅ Time complexity: O(N)​
✅ Space: O(1) extra​
✅ Much cleaner than simulation​
✅ Exactly what the problem wants
This is contest-level greedy thinking, not beginner stuff.
Speed Contest 5
A

Solve 1

#include <bits/stdc++.h>
using namespace std;
#define fast \
ios::sync_with_stdio(false); \
[Link](nullptr);
#define ll long long
#define haa cout << "Yes\n";
#define naa cout << "No\n";
#define endl cout << "\n";
#define nl "\n"

void solve()
{
int x, y;
cin >> x >> y;
int gap = x - 1;
int k = y / gap;
cout << k << nl;
}

int main()
{
fast;
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}

Solve 2

int main()
{
khela_hobe;
barbar_sudhu_amar_uporei_KENOO
{
ll n;
cin >> n;

vector<ll> a(n);

for (ll i = 0; i < n; i++)


cin >> a[i];

ll res = 0;

sort([Link](), [Link]());

ll counter = 0;
for (ll i = 0; i < n; i++)
{
// cout << a[i] << "-" << counter << br;
res += abs(a[i] - counter);
counter++;
}

cout << res << br;


}
tata
}

Solve 3

// D
#include <bits/stdc++.h>
using namespace std;

#define ll long long


#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define nl '\n'

const ll INF = 1e18;


const int MOD = 1e9 + 7;

void solve() {
int x, y;
cin >> x >> y;
cout << y / (x - 1) << nl;
}

int main() {
ios::sync_with_stdio(false);
[Link](nullptr);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
E

Solve 1

// gcd lcm
#include <bits/stdc++.h>
using namespace std;

#define ll long long


#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define nl '\n'

const ll INF = 1e18;


const int MOD = 1e9 + 7;

ll lcm(ll a, ll b) {
return (a / __gcd(a, b)) * b;
}

void solve() {
ll x, y, k;
cin >> x >> y >> k;
while (k--) {
if (x == y)
break;
if (x > y) {
x = __gcd(x, y);
y = lcm(x, y);
} else {
y = __gcd(x, y);
x = lcm(x, y);
}
}
cout << x + y << nl;
}

int main() {
ios::sync_with_stdio(false);
[Link](nullptr);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
CodeForces Div-3
Dashboard - Codeforces Round 1072 (Div. 3)
😐 Binary String Problems
C. Prepend and Append

#include <bits/stdc++.h>
using namespace std;

// [Link]

//CF rating 800

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
int n;
cin >> n;

string s;
cin >> s;

int l = 0, r = n - 1;
bool flag = true;

while (l <= r)
{
if (s[l] == '1' && s[r] == '1' || s[l] == '0' && s[r] == '0')
{
flag = false;
break;
}
else
{
l++;
r--;
}
}

// cout << "size of present L : " << l << "\n";

int ans = 0;

if (!flag)
{
ans = n - (l*2);
// cout << "size of the given string was : " << n << "\n";
cout << ans << "\n";
}
else
{
// cout << "size of the given string was : " << n << "\n";

cout << 0 << "\n";


}
}
return 0;
}
🤬 Difference Array Problems
C. Little Girl and Maximum Sum

C. Two TVs

B. Karen and Coffee


Let's restate and solve “B. Karen and Coffee” step-by-step with a clear explanation and
optimized C++ solution.

🧩 Problem Breakdown
You are given:

●​ n recipes, each suggesting a brewing temperature range [li, ri]​

●​ A temperature is admissible if at least k recipes include it in their range​

●​ You need to answer q queries:​


For each range [a, b], count how many admissible temperatures are inside it.​

⚙️ Constraints
●​ 1 ≤ n, q ≤ 2e5​

●​ 1 ≤ li ≤ ri ≤ 2e5​

●​ Temperatures range: [1, 200000]​

That’s large → O(n * 200000) would be too slow.​


We need O(n + q + maxTemp) solution.

💡 Idea: Prefix Sum + Difference Array


We’ll use difference array + prefix sum technique, similar to “range frequency counting”.
Step 1️⃣: Count how many recipes cover each temperature

We create an array count[200001].

For each range [li, ri]:

count[li] += 1;
count[ri + 1] -= 1;

Then, compute prefix sums:

for (int i = 1; i <= MAX; i++)


count[i] += count[i - 1];

Now count[i] = number of recipes recommending temperature i.

Step 2️⃣: Mark admissible temperatures

Create another array admissible[i] = 1 if count[i] >= k else 0.

Step 3️⃣: Prefix sum again for fast query

Compute:

pref[i] = pref[i - 1] + admissible[i];

Now each query [a, b] can be answered in O(1):

answer = pref[b] - pref[a - 1];

🧠 Complexity
Step Complexity

Building difference array O(n)

Prefix sums O(MAX) = O(200000)

Answering q queries O(q)

Total O(n + q + MAX) ✅


Perfect for the limits.

#include <bits/stdc++.h>
using namespace std;

const int MAX = 200000;

int main() {
ios::sync_with_stdio(false);
[Link](nullptr);

int n, k, q;
cin >> n >> k >> q;

vector<int> diff(MAX + 2, 0);

// Step 1: Build difference array from all recipes


for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
diff[l] += 1;
diff[r + 1] -= 1;
}

// Step 2: Build actual count array using prefix sum


for (int i = 1; i <= MAX; i++) {
diff[i] += diff[i - 1];
}

// Step 3: Build prefix sum for admissible temperatures


vector<int> pref(MAX + 1, 0);
for (int i = 1; i <= MAX; i++) {
int add = 0; // by default, assume not admissible

// if this temperature is recommended by at least k recipes


if (diff[i] >= k) {
add = 1; // it’s admissible
}

// build running total of admissible temperatures


pref[i] = pref[i - 1] + add;
}

// Step 4: Answer each query in O(1)


for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;

int result = pref[b] - pref[a - 1];


cout << result << "\n";
}

return 0;
}
🥱 Prefix_Sum
Prefix Sum

CSES - Subarray Sums II

2D Prefix Sum

CSES - Forest Queries

Problem - 1722E - Counting Rectangles Codeforces


🤤 Mastering STL
STL Master Class -1

B2. The Strict Teacher (Hard Version)

KeyTopic : STL , Upperbound, lowerbound

#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin>>t;

while(t--)
{
int n,m,q;

cin>>n>>m>>q;

vector<int> b(m);

for(auto &val : b)
{
cin>>val;
}

sort([Link](),[Link]());

while(q--)
{
int a;
cin>>a;
auto it = upper_bound([Link](),[Link](), a);

if(it == [Link]())
{
it--;
cout<< n - *it<<"\n";

}
else if(it == [Link]())
{

cout<< *it -1 <<"\n";

}
else
{
int porer_teacher = *it;
it--;
int ager_teacher = *it;
int len = (porer_teacher - ager_teacher) -1;

cout<<(len + 1)/2<<"\n";

}
}

}
return 0;
}

Explanation of the Solution


We have a line of cells from 1 to n, teachers at some positions b[], and for each query David
starts at position a.

David wants to maximize the number of moves before he gets caught.​


Teachers want to minimize it.

👉 The answer is essentially the minimum distance to the nearest teacher, assuming David
moves optimally away.

🔍 Key Logic
1.​ Sort the teachers’ positions.​

2.​ For each query a, use upper_bound to find:​

○​ the first teacher to the right of a​

○​ the teacher to the left (if exists)​

3.​ Three cases:​

Case 1️⃣: No teacher to the right (David is to the right of all teachers)

Closest teacher is the last one:

answer = n - lastTeacher

Case 2️⃣: No teacher to the left (David is left of all teachers)

Closest teacher is the first one:

answer = firstTeacher - 1

Case 3️⃣: David is between two teachers

Teachers at L and R, so free interval length:

len = (R - L) - 1
answer = (len + 1) / 2 // David can run towards center

🧪 Dry Run Example


Let's take this example input:

1
10 3 3
1 4 8
2 3 10

Sorted teacher positions:


b = [1, 4, 8]

Query 1: a = 2

upper_bound(b, 2) → points to 4 (right teacher)​


Left teacher is 1

L = 1, R = 4
len = (4 - 1) - 1 = 2
answer = (2 + 1)/2 = 1

➡ Output: 1

Query 2: a = 3

upper_bound(b, 3) → points to 4​
Left teacher = 1

L = 1, R = 4
len = (4 - 1) - 1 = 2
answer = (2 + 1)/2 = 1
➡ Output: 1

Query 3: a = 10

upper_bound(b, 10) → end (no right teacher)​


Last teacher = 8

answer = n - 8 = 10 - 8 = 2

➡ Output: 2

Final Output:
1
1
2

📌 Why This Works


David survives maximum time by running away from nearest teacher or towards midpoint
between two teachers. This code efficiently uses binary search (upper_bound) so each query
is O(log m).

Total complexity:

Sorting: O(m log m)


Each query: O(log m)

Efficient for constraints up to 2×10⁵.


🎉 Summary
Situation Formula

All teachers left of David n - lastTeacher

All teachers right of David firstTeacher - 1

David between two teachers L and ( (R-L) -1 + 1 ) / 2 =


R (R-L)/2

The code correctly implements this logic.


Map
First Set recap since map is derived from set

Here's a simple example of how to get the number of unique elements using a set in C++.

🟢 Example: Count Unique Elements in an Array


#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> arr = {4, 2, 4, 7, 2, 9, 7};

// Create a set from the vector


set<int> st([Link](), [Link]());

// st now contains only unique elements


cout << "Number of unique elements: " << [Link]() << "\n";

return 0;
}

✔ Explanation

●​ set<int> st([Link](), [Link]());​

○​ A set automatically stores only distinct elements.​

●​ [Link]() returns the count of unique values.​

🔧 Output
For the array {4, 2, 4, 7, 2, 9, 7}, the unique elements are {2, 4, 7, 9} so:

Number of unique elements: 4


Another Usage Example with Strings
string s = "aabccdee";
set<char> st([Link](), [Link]());
cout << "Unique characters: " << [Link]() << "\n";

st will contain: {a, b, c, d, e}​


Output:

Unique characters: 5

Now Map

Here's a clear example of how to find the frequency of each unique element using a map in
C++.

🟢 Example: Frequency of Elements in an Array


#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> arr = {4, 2, 4, 7, 2, 9, 7, 7};

map<int, int> freq;

// Counting frequencies
for(int x : arr) {
freq[x]++; // increment count for element x
}

// Printing results
cout << "Element -> Frequency\n";
for(auto &p : freq) {
cout << [Link] << " -> " << [Link] << "\n";
}

return 0;
}

✔ Explanation

●​ map<int, int> freq;​

○​ Maps each value → number of times it appears.​

●​ freq[x]++;​

○​ When accessing a key that doesn't exist, map will create it with value 0, then
increment.​

●​ Loop prints each unique element and its count, sorted by the key automatically.​

🔧 Output for the example


Array: {4, 2, 4, 7, 2, 9, 7, 7}

Element -> Frequency


2 -> 2
4 -> 2
7 -> 3
9 -> 1

📌 Example with Strings (character frequencies)


string s = "aabccdee";

map<char, int> freq;

for(char c : s) {
freq[c]++;
}

for(auto &p : freq) {


cout << [Link] << " -> " << [Link] << "\n";
}

Output:

a -> 2
b -> 1
c -> 2
d -> 1
e -> 2

📌 Pick one:
1️⃣ unordered_map version (faster frequency counting)​
2️⃣ Find the most frequent element​
3️⃣ Find the least frequent element​
4️⃣ Sort elements by frequency (highest → lowest)​
5️⃣ Sort elements by frequency (lowest → highest)​
6️⃣ Frequency of words in a sentence
Here is a simple example of counting the frequency of unique elements using unordered_map
in C++:

✅ Example: Frequency Count with unordered_map


#include <bits/stdc++.h>
using namespace std;

int main() {
vector<int> arr = {1, 2, 2, 3, 3, 3, 4, 5, 5};

unordered_map<int, int> freq;

// Count frequency
for(int x : arr) {
freq[x]++;
}

// Print frequencies
cout << "Frequency of elements:\n";
for(auto &p : freq) {
cout << [Link] << " -> " << [Link] << "\n";
}

return 0;
}

🧾 Output (order may vary due to hashing)


Frequency of elements:
1 -> 1
2 -> 2
3 -> 3
4 -> 1
5 -> 2

🔍 Why unordered_map?
Feature unordered_map

Time complexity average O(1) for insert/search

Order of keys Not sorted

Best use case Fast frequency


counting

Here is a clear example where we are given a vector<string> containing multiple words,
and we find the frequency of each unique string using map (which keeps keys sorted).

📌 Example: Counting frequency of strings using map


#include <bits/stdc++.h>
using namespace std;

int main() {
vector<string> words = {
"apple", "banana", "apple", "orange", "banana", "apple",
"grape"
};

map<string, int> freq;

// Count frequency of each string


for (auto &w : words) {
freq[w]++;
}

// Print results
cout << "Frequency of strings:\n";
for (auto &p : freq) {
cout << [Link] << " -> " << [Link] << "\n";
}

return 0;
}

🧾 Output:
Frequency of strings:
apple -> 3
banana -> 2
grape -> 1
orange -> 1

Feature Value

Stores unique Yes


keys

Keys remain ✔️ Yes (lexicographically for


sorted strings)

Time complexity O(log N) per insert/search

examples for:

🔸 Counting frequencies of characters in a sentence​


🔸 Counting word occurrences from user input​
🔸 Case-insensitive frequency counting​
🔸 Using maps for pair/string combinations​
🔸 Sorting by frequency instead of alphabet
String
String Cutting

string s = “abcdef”

[Link](0,3) - output - abc


Multiset
Erase Operation Doesn’t work on multiset

Set can be made into multiset


Vector
Necessary Operations That Can be done on vector

Input

Output

Traverse
😷 PBDS-Ordered Set
PBDS
(Ordered set)

D. Counting Pairs

// CF-Rating-1200

//

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;

// // for set

template <typename T>


using pbds = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;

// // for multiset

// template <typename T>


// using pbds = tree<T, null_type, less_equal<T>, rb_tree_tag,
tree_order_statistics_node_update>;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin >> t;

while (t--)
{
long long int n, x, y;
cin >> n >> x >> y;

long long int sum = 0,ans = 0;;

vector<long long int> v(n);

pbds<pair<long long int, long long int>> st;

long long int l = 0, r = 0;

for (int i = 0; i < n; i++)


{

cin >> v[i];

[Link]({v[i],i});

sum += v[i];
}

for (int i = 0; i < n; i++)


{
l = (sum - v[i]) - y;
r = (sum - v[i]) - x;

[Link]({v[i],i});

ans += st.order_of_key({r+1,i}) - st.order_of_key({l,i});

}
cout << ans << "\n";
}

return 0;
}

F. Greetings

Josephus Problem I

Explanation - topicwise class - 6 - 40.00 min timestamp

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;

template <typename T>


using pbds = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;

int main()
{

ios::sync_with_stdio(false);
[Link](nullptr);

int n;

cin>>n;

pbds<int> s;

for (int i = 1; i <= n; i++)


[Link](i);

int idx = 1 % n;

while (n--)
{
auto it = s.find_by_order(idx);
cout<<*it<<" ";
[Link](it);

if(n)
{
idx = (idx + 1) % n;
}
}

return 0;
}
Josephus Problem II

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

using namespace std;

template <typename T>


using pbds = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;

int main()
{

ios::sync_with_stdio(false);
[Link](nullptr);

long long int n,k;

cin>>n>>k;

pbds<int> s;

for (int i = 1; i <= n; i++)


[Link](i);

int idx = k % n;

while (n--)
{
auto it = s.find_by_order(idx);
cout<<*it<<" ";
[Link](it);

if(n)
{
idx = (idx + k) % n;
}
}

return 0;
}
😁 Other Stuff
Modular Arithmetic

[Link]
MEX
🤕 Basic Math Problems
[Link]

Solve 1

#include <bits/stdc++.h>
using namespace std;

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;

cin>>t;

while (t--)
{
int n;
cin>>n;

vector<int> a(n);

for (int i = 0; i < n; i++)


{
cin>>a[i];
}

sort([Link](),[Link]());

cout<<a[n-1]<<"\n";
}

return 0;
}

Explanation

First: understand the problem in plain human words


●​ All numbers are positive (1 ≤ ai ≤ 10)​

●​ You want the maximum average of any contiguous subarray​

●​ Answer is guaranteed to be an integer​

🧠 Key human observation (before code)


When all numbers are positive,​
adding more elements to a subarray can never increase the average above
the maximum element inside it.

Why?

Because average is always between min and max of the elements in the subarray.

So the best possible average is achieved by:

👉 taking a subarray of length 1, containing the largest element.


That’s why your shortcut works:
sort([Link](), [Link]());
cout << a[n-1];

Chef and Battery

Solve

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
int n;

cin>>n;

int ans = 0;

while(n != 50)
{
if(n<50)
{
n = n + 2;
ans++;
}
else if(n>50)
{
n = n - 3;
ans++;

}
}

cout<<ans<<"\n";
}
return 0;
}

CF-Div-3- A - A. Shizuku Hoshikawa and Farm Legs

Solve 1

#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
int n;
cin>>n;
int cnt=0;
for (int i = 0; i <=n; i+=2)
{
int rem=n-i;
if(rem%4==0) cnt++;

}
cout << cnt << "\n";
}

int main() {
ios::sync_with_stdio(false);
[Link](NULL);

int t; cin >> t;


while (t--) {
solve();
}

return 0;
}

Solve 2
😗 implementation
Rating 800

[Link]
🙉 1500+,1600+ rated problems
Rating - 1638

[Link]
1600+ Rated
1500+ Rated
🤐 Math & Number Theory
Week 6 -

[Link]

My Solve

#include <bits/stdc++.h>
using namespace std;

int LCM(long int a,long int b)


{

return (a / __gcd(a, b)) * b;


}

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;

cin >> t;

while (t--)
{
long long a, b;

cin >> a >> b;

cout<<LCM(a,a) - __gcd(b,a)<<"\n";
}
return 0;
}
Solve 2 (by others)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'

int main() {
ios_base::sync_with_stdio(0); [Link](0);
int tc;
cin >> tc;
while (tc--)
{
long long int a, b;
cin >> a >> b;
if (b % a == 0)
{
long long int x = b;
long long int lcm = (a * x) / __gcd(a, x);
cout << lcm - __gcd(b, x) << endl;
}
else {
long long int x = __gcd(a, b);
long long int lcm = (a * x) / (__gcd(a, x));
long long int gcd = __gcd(b, x);
cout << lcm - gcd << endl;
}

}
return 0;
}
P

[Link]

#include <bits/stdc++.h>
using namespace std;

//CC-*1108

int main()
{
ios::sync_with_stdio(false);
[Link](nullptr);

int t;
cin>>t;

while(t--)
{
int n;
cin>>n;

vector<int> a(n);

bool even_found = false;

for (int i = 0; i < n; i++)


{
cin>>a[i];
if(a[i] % 2 == 0)
{
even_found = true;

}
}

if(even_found)
{
cout<<"NO"<<"\n";
}
else
{
cout<<"YES"<<"\n";
}

}
return 0;
}

Explanation

🧠 Chef vs Doof — HUMAN MODE


EDITORIAL
Problem in simple words
Chef has some numbers.

Dr Doof will destroy all even numbers.

Chef wants to stop him by finding one odd number that is:

●​ a multiple of every number Chef has​


If Chef can find such an odd number → print YES​
Otherwise → print NO

Step 1️⃣ What does “multiple of all numbers” really mean?


If Chef has numbers:

[ a1, a2, a3, ... ]

Chef wants a number X such that:

X % a1 == 0
X % a2 == 0
X % a3 == 0
...

In other words:

X must be divisible by every given number

Step 2️⃣ Key restriction: X must be ODD


This is very important.

So X must look like:

1, 3, 5, 7, 9, ...

No even numbers allowed.

Step 3️⃣ Think like a human: try small examples


Example 1
Numbers: [1, 2, 5, 4, 3]

Let’s look at them:

●​ 2 → even​

●​ 4 → even​

Question:

Can an odd number be divisible by 2 or 4?


Answer:​
No

Why?

●​ Any number divisible by 2 is even​

●​ Any number divisible by 4 is also even​

So an odd number can never be a multiple of an even number.

👉 Immediately impossible
Answer: NO

Example 2
Numbers: [7]

All numbers are odd.

Try:

●​ 7 → odd and divisible by 7 ✅​


●​ 21 → odd and divisible by 7 ✅​

●​ 49 → odd and divisible by 7 ✅​


👉 Possible
Answer: YES

Step 4️⃣ The BIG observation (human logic)


❗ If even ONE number in the list is even, the answer is NO
Because:

●​ Any multiple of an even number is even​

●​ Chef needs an odd multiple​

●​ Impossible combination​

Step 5️⃣ What if all numbers are odd?


Then it’s always possible.

Why?

●​ LCM of odd numbers is odd​

●​ That LCM itself is a valid odd multiple​

Example:

[3, 5, 7]
LCM = 105 (odd)

👉 YES
So:​

Step 6️⃣ Final human rule (memorize this)


Condition Answer

At least one even number NO


present

All numbers are odd YES

That’s it.​
No need to calculate LCM.​
No need to build big numbers.

🧠 Final takeaway (very important)


Whenever you see:

“find an odd multiple”

divisibility

GCD / LCM

👉 First check parity (odd/even)


It often kills the problem instantly.
[Link] To C
[Link] to C++
Module 5

String built-in functions:

1.​ Capacity
a.​ [Link]() -> returns the size of the string.
b.​ s.max_size() -> returns the maximum size that string can hold.
c.​ [Link]() -> returns current available capacity of the string.
d.​ [Link]() -> clear the string.
e.​ [Link]() -> return true/false if the string is empty.
f.​ [Link]() -> change the size of the string.
2.​ Element access
a.​ S[i] -> access the ith index of the string.
b.​ [Link](i) -> access the ith index of the string.
c.​ [Link]() -> access the last element of the string.
d.​ [Link]() -> access the first element of the string.
3.​ Modifiers
a.​ s+= -> append another string.
b.​ [Link]() -> append another string.
c.​ s.push_back() -> add character to the last of the string.
d.​ s.pop_back() -> remove the last character of the string.
e.​ s= -> assign string.
f.​ [Link]() -> assign string.
g.​ [Link]() -> erase characters from the string.
h.​ [Link]() -> replace a portion of the string.
i.​ [Link]() -> insert a portion to a specific position.
4.​ Iterators
a.​ [Link]() -> pointer to the first element.
b.​ [Link]() -> pointer to the next element after the last element of the
string.
Stringstream

#include<bits/stdc++.h>
using namespace std;

int main()
{
string s;
getline(cin,s);

stringstream ss(s);

string word;

while(ss >> word)


{
cout<<word<<endl;
}

return 0;
}
[Link]-Data-Structures
Vector Built-in Functions:

1.​ Initialization

Name Details Time


Complexity

vector<type>v; Construct a vector with 0 elements. O(1)

vector<type>v(N); Construct a vector with N O(N)


elements.

vector<type>v(N,V); Construct a vector with N elements O(N)


and the value will be V.

vector<type>v(v2); Construct a vector by copying O(N)


another vector v2.

vector<type>v(A,A+N); Construct a vector by copying all O(N)


elements from an array A of size N.

2.​ Capacity

Name Details Time Complexity

[Link]() Returns the size of the O(1)


vector.

v.max_size() Returns the maximum size O(1)


that the vector can hold.

[Link]() Returns the current O(1)


available capacity of the
vector.

[Link]() Clears the vector O(N)


elements. Do not delete
the memory, only clear the
value.

[Link]() Return true/false if the O(1)


vector is empty or not.

[Link]() Change the size of the O(K); where K is the


vector. difference between new
size and current size.
3.​ Modifiers

Name Details Time Complexity

v= or [Link]() Assign another vector. O(N) if sizes are different,


O(1) otherwise.

v.push_back() Add an element to the end. O(1)

v.pop_back() Remove the last element. O(1)

[Link]() Insert elements at a O(N+K); where K is the


specific position. number of elements to be
inserted.

[Link]() Delete elements from a O(N+K); where K is the


specific position. number of elements to be
deleted.

replace([Link](),v Replace all the value with O(N)


.end(),value,replac replace_value. Not under a
e_value) vector.

find([Link](),[Link] Find the value V. Not under O(N)


d(),V) a vector.

4.​ Element access

Name Details Time Complexity

v[i] Access the ith element. O(1)

[Link](i) Access the ith element. O(1)

[Link]() Access the last element. O(1)

[Link]() Access the first element. O(1)


5.​ Iterators

Name Details Time Complexity

[Link]() Pointer to the first element. O(1)

[Link]() Pointer to the last element. O(1)


Module 2 Extra Practice Problem

1.​ L. New Array (Solve using vector)

Using vector

#include<bits/stdc++.h>
using namespace std;

void concat(int s)
{
vector<int> a(s);

for (int i = 0; i < s; i++)


{
cin>>a[i];
}

vector<int> b(s);

for (int i = 0; i < s; i++)


{
cin>>b[i];
}

[Link]([Link](),[Link](),[Link]());

vector<int> c;
[Link]([Link](),[Link](),[Link]());

for (int i = 0; i < [Link](); i++)


{
cout<<c[i]<<" ";
}
}

int main()
{

int n;
cin>>n;

concat(n);

return 0;
}
Using Basic Array and manual copying

#include<bits/stdc++.h>
using namespace std;

void concat(int s)
{
int a[s];

for (int i = 0; i < s; i++)


{
cin>>a[i];
}

int b[s];

for (int i = 0; i < s; i++)


{
cin>>b[i];
}

int c[2*s];

for (int i = 0; i < s; i++)


{
c[i] = b[i] ;
}

for (int i = 0; i < s; i++)


{
c[i+s] = a[i] ;
}
for (int i = 0; i < 2*s; i++)
{
cout<<c[i]<<" ";
}
}

int main()
{

int n;
cin>>n;

concat(n);

return 0;
}
2.​ C. Replacement (Solve using vector)

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

vector<int> a(n);

for (int i = 0; i < n; i++)


{
cin>>a[i];
}

for (int i = 0; i < n; i++)


{
if(a[i] < 0 )
{
a[i] = 2;
}
else if(a[i]>0)
{
a[i] = 1;
}
else
{
a[i] = 0;
}
}

for (int i = 0; i < n; i++)


{
cout<<a[i]<<" ";
}

return 0;
}
Topics:
1.​ Vector

Codeforces Problem Links:

1.​ F. Reversing (Solve by reversing the vector)

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin>>v[i];
}

for (int i = 0; i < n/2; i++)


{
int temp = v[i];
v[i] = v[n-1-i];
v[n-1-i] = temp;
}

for (int i = 0; i < n; i++)


{
cout<<v[i]<<" ";
}

return 0;
}
2.​ C. Replacement (Solve using vector)

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

vector<int> a(n);

for (int i = 0; i < n; i++)


{
cin>>a[i];
}

for (int i = 0; i < n; i++)


{
if(a[i] < 0 )
{
a[i] = 2;
}
else if(a[i]>0)
{
a[i] = 1;
}
else
{
a[i] = 0;
}
}

for (int i = 0; i < n; i++)


{
cout<<a[i]<<" ";
}

return 0;
}

3.​ D. Counting Elements (Solve using vector and built in functions)


4.​ J. Count Letters (Use vector as frequency array)

5.​ Y. Range sum query (You’ll get TLE, no problem, don’t ask for support, we will solve
it on next module)

6.​ After solving each problem, calculate your solution’s time complexity.
#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;
vector<int> v(n);
for (int i = 0; i < n; i++)
{
cin>>v[i];
}

for (int i = 0; i < n/2; i++)


{
int temp = v[i];
v[i] = v[n-1-i];
v[n-1-i] = temp;
}

for (int i = 0; i < n; i++)


{
cout<<v[i]<<" ";
}

return 0;
}
Module-3-

3-6- [Link] Search

Brute Force Solve -With TLE

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n,q;
cin>>n>>q;

int a[n];

for (int i = 0; i < n; i++)


{
cin>>a[i];
}

for (int i = 0; i < q; i++)


{
int x;
cin>>x;
int flag = 0;

for (int i = 0; i < n; i++)


{
if(a[i]==x)
{
flag = 1;
}
}
if(flag==1)
{
cout<<"found"<<endl;
}
else
{
cout<<"not found"<<endl;
}

return 0;
}
Module 4 - Exam

[Link]
es
Module-5
a.​ Why do you think linked-list requires more memory than an array when storing
the same number of elements?

b.​ Write down Three Limitations of the array which can be solved by the use of
Linked List

c.​ What is the value of Head?


d.​ What is the value of ? marked address location?
e.​ What will be the value of Head->Next->Next->Value?
f.​What will be the value of Sum following pseudocode snippets?
Sum = 0
Temp = Head
While ( Temp -> Next!= 1020){
​ Sum += Temp-> value
​ Temp = Temp -> Next
}
Sum -= Temp -> value;

Module-6
Implement the following Operations for Singly Linked List

1.​ Create a Singly Linked List (Take input from user)


#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

void insert_at_tail(Node* &head,Node* &tail,int val)


{
Node* newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode; //corner case
return;
}

tail->next = newnode;
tail = tail->next;

void print_linked_List(Node* head)


{
Node* temp = head;
while(temp != NULL)
{
cout<<temp->val<<endl;
temp = temp->next;

}
}

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while (true) //infinite loop //O(n)
{
cin>>val;
if(val == -1)
{
break;//break condition for infinite loop
}
insert_at_tail(head,tail,val);//O(1)
}

print_linked_List(head);

return 0;
}

2.​ Count the Size of the list

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

int count_size_of_linked_list(Node * head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{

temp = temp->next;
count ++;

}
return count;
}

void print_linked_list(Node* head)


{
Node* temp = head;
while(temp !=NULL)
{
cout<<temp->val<<endl;
temp = temp->next;
}
}

void insert_in_linked_list(Node* &head,Node* &tail,int val)


{
Node * newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;

}
tail->next= newnode;
tail = tail->next;

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(1)
{
cin>>val;
if(val==-1)
{
break;
}
insert_in_linked_list(head,tail,val);

// print_linked_list(head);

int size = count_size_of_linked_list(head);

cout<<size<<endl;

return 0;
}
3.​ Display List

4.​ Insertion at Head

5.​ Insertion at Tail

6.​ Insertion at Specific Position

Introduction to Basic Data Structures

Module 6.5: Practice Day 01


(Practice Questions)
Topics:
1.​ Singly Linked List

Question: Create a singly linked list and print the size of the linked list.

Sample Linked List Sample Output


2153489 7
5145 4

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

int count_size_of_linked_list(Node * head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{

temp = temp->next;
count ++;

}
return count;
}
void print_linked_list(Node* head)
{
Node* temp = head;
while(temp !=NULL)
{
cout<<temp->val<<endl;
temp = temp->next;
}
}

void insert_in_linked_list(Node* &head,Node* &tail,int val)


{
Node * newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;

}
tail->next= newnode;
tail = tail->next;

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(1)
{
cin>>val;
if(val==-1)
{
break;
}
insert_in_linked_list(head,tail,val);

// print_linked_list(head);

int size = count_size_of_linked_list(head);

cout<<size<<endl;

return 0;
}
Question: Create a singly linked list and check if the linked list contains any
duplicate value. You can assume that the maximum value will be 100.

Sample Linked List Sample Output


548621 NO

245674 YES

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

int Find_duplicate_in_linked_list(Node * head)


{
Node* temp = head;
int freq[101] = {0};
int dup = 0;
while(temp !=NULL)
{

freq[temp->val]++;
temp = temp->next;

for (int i = 0; i < 101; i++)


{
if(freq[i] > 1 )
{
dup = 1;
break;
}
}
return dup ;

}
void print_linked_list(Node* head)
{
Node* temp = head;
while(temp !=NULL)
{
cout<<temp->val<<endl;
temp = temp->next;
}
}

void insert_in_linked_list(Node* &head,Node* &tail,int val)


{
Node * newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;

}
tail->next= newnode;
tail = tail->next;

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(1)
{
cin>>val;
if(val==-1)
{
break;
}
insert_in_linked_list(head,tail,val);

// print_linked_list(head);

int find = Find_duplicate_in_linked_list(head);

if(find == 1)
{
cout<<"YES";
}
else if(find == 0)
{
cout<<"NO";
}

return 0;
}
Question: Create a singly linked list and print the middle element. If there are
multiple values in the middle print both.

Sample Linked List Sample Output


2 4 6 8 10 6

123456 34

#include<bits/stdc++.h>
using namespace std;

//Practice Module 6.5 Problem-1

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

int count_size_of_linked_list(Node * head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{

temp = temp->next;
count ++;

}
return count;
}

void print_linked_list(Node* head)


{
Node* temp = head;
while(temp !=NULL)
{
cout<<temp->val<<endl;
temp = temp->next;
}
}

void insert_in_linked_list(Node* &head,Node* &tail,int val)


{
Node * newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;

}
tail->next= newnode;
tail = tail->next;

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(1)
{
cin>>val;
if(val==-1)
{
break;
}
insert_in_linked_list(head,tail,val);

// print_linked_list(head);

int size = count_size_of_linked_list(head);

int mid = size /2;


// cout<<size<<endl;
// cout<<mid<<endl;

Node * temp = head;

if(size%2 != 0)
{
for (int i = 1; i <= mid; i++)
{
temp = temp->next;
if(i==mid)
{
cout<<temp->val;
}
}
}
else
{
for (int i = 1; i <= mid-1; i++)
{
temp = temp->next;
if(i==mid-1)
{
cout<<temp->val<<" "<<temp->next->val;
}
}

}
return 0;
}

Question: Create a singly linked list and check if the linked list is sorted in
ascending order.

Sample Linked List Sample Output


15689 YES

246584 NO
#include<bits/stdc++.h>
using namespace std;

//Practice Module 6.5 Problem-4

class Node
{
public:
int val;
Node* next;
Node(int val)
{
this->val = val;
this->next = NULL;
}
};

int count_size_of_linked_list(Node * head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{

temp = temp->next;
count ++;

}
return count;
}

void print_linked_list(Node* head)


{
Node* temp = head;
while(temp !=NULL)
{
cout<<temp->val<<endl;
temp = temp->next;
}
}

void insert_in_linked_list(Node* &head,Node* &tail,int val)


{
Node * newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;

}
tail->next= newnode;
tail = tail->next;

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(1)
{
cin>>val;
if(val==-1)
{
break;
}
insert_in_linked_list(head,tail,val);

// print_linked_list(head);
int size = count_size_of_linked_list(head);

int find = 0;

Node * temp = head;

while(temp != NULL && temp->next != NULL)


{

if(temp->val>temp->next->val)
{
find = 1;
break;
}
temp = temp->next;

if(find==1)
{
cout<<"NO";
}
else
{
cout<<"YES";
}

return 0;
}
Introduction to Basic Data Structures

Module 7.5: Practice Day 02


(Practice Questions)
Topics:
1.​ Singly Linked List

Question: Take two singly linked lists as input and check if their sizes are same or
not.

Sample Input Sample Output


2 1 5 3 4 9 -1 YES
1 2 3 4 5 6 -1

5 1 4 5 -1 NO
5 1 4 -1
Question: Take a singly linked list as input and print the reverse of the linked list.

Sample Input Sample Output


5 4 8 6 2 1 -1 126845

1 2 3 4 -1 4321
Question: Take a singly linked list as input, then print the maximum value of
them.

Sample Input Sample Output


2 4 1 3 5 4 2 5 -1 5

5 4 1 2 5 6 8 4 1 3 -1 8
Question: Take a singly linked list as input, then take q queries. In each query you
will be given an index and value. You need to insert those values in the given index
and print the linked list. If the index is invalid print “Invalid”.

Sample Input Sample Output


10 20 30 -1​ 10 40 20 30
7 Invalid
1 40 10 40 20 30 50
5 50
100 10 40 20 30 50
4 50
0 100 Invalid
7 40 100 110 10 40 20 30 50
1 110 100 110 10 40 20 30 50 40
7 40
Question: Take a singly linked list as input and sort it in descending order. Then
print the list.

Sample Input Sample Output


1 4 5 2 7 -1 75421

20 40 30 10 50 60 -1 60 50 40 30 20 10
Module 8 - Exam

[Link]
s-a-batch-07/challenges
Module - 9 : Doubly Linked List

Comparison of all operation's complexity


between Array, Singly and Doubly linked list:

Operation Array Singly Doubly

Insert at Head O(N) O(1) O(1)

Insert at Tail O(1) O(1) O(1)

Insert at any Position O(N) O(N) O(N)

Delete at Head O(N) O(1) O(1)

Delete at Tail O(1) O(N) O(1)

Delete at any Position O(N) O(N) O(N)


List Built-in Functions:

1.​ Constructor

Name Details Time


Complexity

list<type>myList; Construct a list with 0 elements. O(1)

list<type>myList(N); Construct a list with N elements O(N)


and the value will be garbage.

list<type>myList(N,V); Construct a list with N elements O(N)


and the value will be V.

list<type>myList(list2); Construct a list by copying another O(N)


list list2.

list<type>myList(A,A+ Construct a list by copying all O(N)


N); elements from an array A of size N.

list<type>myList([Link] Construct a list by copying all O(N)


n(),[Link]()); elements from a vector v.

2.​ Capacity

Name Details Time Complexity

[Link]() Returns the size of the list. O(1)

myList.max_size() Returns the maximum size O(1)


that the list can hold.

[Link]() Clears the list elements. O(N)

[Link]() Return true/false if the list O(1)


is empty or not.

[Link]() Change the size of the list. O(K); where K is the


difference between new
size and current size.
3.​ Modifiers

Name Details Time Complexity

myList= or Assign another list. O(N)


[Link](list
[Link](),[Link](
))

myList.push_back Add an element to the tail. O(1)


()

myList.push_front Add an element to the O(1)


() head.

myList.pop_back() Delete the tail. O(1)

myList.pop_front() Delete the head. O(1)

[Link]() Insert elements at a O(N+K); where K is the


specific position. number of elements to be
inserted.

[Link]() Delete elements from a O(N+K); where K is the


specific position. number of elements to be
deleted.

replace([Link] Replace all the value with O(N)


gin(),[Link](), replace_value. Not under a
value,replace_val list STL.
ue)

find([Link]( Find the value V. Not under O(N)


),[Link](),V) a list STL.
4.​ Operations

Name Details Time Complexity

[Link](V) Remove the value V from O(N)


the list.

[Link]() Sort the list in ascending O(NlogN)


order.

[Link](greate Sort the list in descending O(NlogN)


r<type>()) order

[Link]() Deletes the duplicate O(N), with sort O(NlogN)


values from the list. You
must sort the list first.

[Link]() Reverse the list. O(N)

5.​ Element access

Name Details Time Complexity

[Link]() Access the tail element. O(1)

[Link]() Access the head element. O(1)

next([Link] Access the ith element O(N)


(),i)

6.​ Iterators
Name Details Time Complexity

[Link]() Pointer to the first element. O(1)

[Link]() Pointer to the last element. O(1)

Introduction to Basic Data Structures

Module 10.5: Practice Day 01


(Practice Questions)

Topics:
1.​ Doubly Linked List
Question: Take two doubly linked lists as input and check if they are the same or
not.

Sample Input Sample Output


10 20 30 40 50 -1 YES
10 20 30 40 50 -1

10 20 30 40 50 -1 NO
10 20 30 40 -1

10 20 30 40 -1 NO
10 20 30 40 50 -1

10 20 30 40 -1 NO
40 30 20 10 -1

1 2 3 4 5 -1 NO
5 4 1 2 6 -1

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* next;
Node* prev;

Node(int val)
{
this->val = val;
this->next = NULL;
this->prev = NULL;
}
};

void insert_at_tail(Node* &head,Node* & tail,int val)


{
Node* newnode =new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
newnode->prev = tail;
tail = newnode;
}

void print_doubly_linked_list_forward(Node* head)


{
Node* temp = head;
while(temp != NULL)
{
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}

int size_of_doubly_linked_list(Node* head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{
temp = temp->next;
count++;
}
return count;
}

int check_if_doubly_linked_list_same(Node* head1,Node* head2,int size)


{
Node* temp1 = head1;
Node* temp2 = head2;
int flag = 0;
for (int i = 0; i < size; i++)
{
if(temp1->val != temp2->val)
{
flag = 1;
break;
}
temp1 = temp1->next;
temp2 = temp2->next;

}
return flag;
}
int main()
{

Node* head1 = NULL;


Node* tail1 = NULL;
int val1;
while(true)
{
cin>>val1;
if(val1==-1)
{
break;
}
insert_at_tail(head1,tail1,val1);
}

Node* head2 = NULL;


Node* tail2 = NULL;
int val2;
while(true)
{
cin>>val2;
if(val2==-1)
{
break;
}
insert_at_tail(head2,tail2,val2);
}

print_doubly_linked_list_forward(head1);
print_doubly_linked_list_forward(head2);

int size1 = size_of_doubly_linked_list(head1);

int size2 = size_of_doubly_linked_list(head2);

if(size1 != size2)
{
cout<<"NO"<<endl;
}
else if(size1 == size2)
{
int same = check_if_doubly_linked_list_same(head1,head2,size1);
if(same == 1)
{
cout<<"NO"<<endl;
}
else if(same == 0)
{
cout<<"YES"<<endl;
}

return 0;
}
Question: Take a doubly linked list as input and reverse it. After that print the
linked list.

Sample Input Sample Output


10 20 30 -1 30 20 10

10 20 30 40 -1 40 30 20 10

#include<bits/stdc++.h>
using namespace std;

//10.5- Practice Problem 2

class Node
{
public:
int val;
Node* next;
Node* prev;
Node(int val)
{
this->val = val;
this->next = NULL;
this->prev = NULL;
}
};

void insert_at_tail(Node* &head,Node* &tail,int val)


{
Node* newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
newnode->prev = tail;
tail = newnode;

void print_doubly_linked_list_forward(Node* head)


{
Node* temp = head;
while(temp != NULL)
{
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;
}

void reverse_doubly_linked_list(Node* &head,Node* &tail)


{

for(Node* i = head ,* j = tail; i != j && i->prev != j; i = i->next,j = j->prev )


{
swap(i->val,j->val);
}

int main()
{
Node* head = NULL;
Node* tail = NULL;

int val;

while(cin>>val)
{
if(val==-1)
{
break;
}
insert_at_tail(head,tail,val);
}

print_doubly_linked_list_forward(head);

reverse_doubly_linked_list(head,tail);

print_doubly_linked_list_forward(head);
return 0;
}

Question: Take a doubly linked list as input and check if it forms any palindrome
or not.

Sample Input Sample Output


10 20 30 20 10 -1 YES
10 20 30 30 20 10 -1 YES

10 20 30 40 20 10 -1 NO

10 20 30 20 40 -1 NO

10 20 30 10 10 -1 NO

10 20 20 20 10 -1 YES

#include<bits/stdc++.h>
using namespace std;

//10.5- Practice Problem 3

class Node
{
public:
int val;
Node* next;
Node* prev;
Node(int val)
{
this->val = val;
this->next = NULL;
this->prev = NULL;
}
};

void insert_at_tail(Node* &head,Node* &tail,int val)


{
Node* newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
newnode->prev = tail;
tail = newnode;

void print_doubly_linked_list_forward(Node* head)


{
Node* temp = head;
while(temp != NULL)
{
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;

int check_palindrome_doubly_linked_list(Node* &head,Node* &tail)


{

int flag = 0;
for(Node* i = head ,* j = tail; i != j && i->prev != j; i =
i->next,j = j->prev )
{
if(i->val != j->val)
{
flag = 1;
}
}
return flag;

int main()
{

Node* head = NULL;


Node* tail = NULL;

int val;

while(cin>>val)
{
if(val==-1)
{
break;
}
insert_at_tail(head,tail,val);
}

int pal = check_palindrome_doubly_linked_list(head,tail);


// print_doubly_linked_list_forward(head);

if(pal == 0)
{
cout<<"YES";
}
else if(pal == 1 )
{
cout<<"NO";
}

return 0;
}
Question: You have a doubly linked list which is empty initially. You need to take
a value Q which refers to queries. For each query you will be given X and V. You
will insert the value V to the Xth index of the doubly linked list and print the list in
both left to right and right to left. If the index is invalid then print “Invalid”.

Sample Input Sample Output


6 10
0 10 10
1 20 10 20
4 30
20 10
0 30
1 40 Invalid
5 50 30 10 20
20 10 30
30 40 10 20
20 10 40 30
Invalid
#include<bits/stdc++.h>
using namespace std;

//10.5- Practice Problem 2

class Node
{
public:
int val;
Node* next;
Node* prev;
Node(int val)
{
this->val = val;
this->next = NULL;
this->prev = NULL;
}
};

void insert_at_head(Node* &head,Node* &tail,int val)


{
Node* newnode =new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;
}
newnode->next = head;
head->prev = newnode;
head = newnode;
}

void insert_at_any(Node* head,int idx,int val)


{
Node* newnode = new Node(val);
Node* temp = head;
for(int i = 1; i < idx ; i++ )
{
temp = temp->next;
}
// 4 connections
newnode->next = temp->next;
temp->next->prev = newnode;
temp->next = newnode;
newnode->prev = temp;

int size_of_doubly_linked_list(Node* head)


{
Node* temp = head;
int count = 0;
while(temp !=NULL)
{
temp = temp->next;
count++;
}
return count;
}

void insert_at_tail(Node* &head,Node* &tail,int val)


{
Node* newnode = new Node(val);
if(head==NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
newnode->prev = tail;
tail = newnode;

void print_doubly_linked_list_forward(Node* head)


{
Node* temp = head;
while(temp != NULL)
{
cout<<temp->val<<" ";
temp = temp->next;
}
cout<<endl;

void print_doubly_linked_list_backward(Node* tail)


{
Node* temp = tail;
while(temp != NULL)
{
cout<<temp->val<<" ";
temp = temp->prev;
}
cout<<endl;

int main()
{

Node* head = NULL;


Node* tail = NULL;
int q;
cin>>q;
for (int i = 0; i < q; i++)
{

int x;
int v;

cin>>x;

cin>>v;

int size = size_of_doubly_linked_list(head);

if(x > size)


{
cout<<"invalid"<<endl;
}
else
{
if(x==0)
{
insert_at_head(head,tail,v);
}
else if(x==size)
{
insert_at_tail(head,tail,v);
}
else if(x<size)
{
insert_at_any(head,x,v);
}
print_doubly_linked_list_forward(head);
print_doubly_linked_list_backward(tail);
}

return 0;
}
Question: Take a doubly linked list as input and sort it in ascending order. Then
print the list.

Sample Input Sample Output


1 4 5 2 7 -1 12457

20 40 30 10 50 60 -1 10 20 30 40 50 60

#include<bits/stdc++.h>
using namespace std;

//10.5- Practice Problem 5

class Node
{
public:
int val;
Node* next;
Node* prev;
Node(int val)
{
this->val = val;
this->next = NULL;
this->prev = NULL;
}
};

//this is basically insert_at_tail


void input_doubly_linked_list(Node* &head,Node* &tail,int val)
{
Node* newnode = new Node(val);
if(head == NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next= newnode;
newnode->prev = tail;
tail = newnode;
}

int main()
{
Node* head = NULL;
Node* tail = NULL;
int val;
while(cin>>val)
{
if(val==-1)
{
break;
}
input_doubly_linked_list(head,tail,val);

vector<int> v;
Node* temp = head;

while(temp != NULL)
{
v.push_back(temp->val);
temp = temp->next;
}

sort([Link](),[Link]());

for(int x:v)
{
cout<<x<<" ";
}

return 0;
}

Module 13- Stack

Stack Built-in Functions:

Name Details Time


Complexity
[Link]() Add an element to the tail/back of the stack. O(1)

[Link]() Delete the last value of the stack. O(1)

[Link]() Access the last element of the stack. O(1)

[Link]() Returns the size of the stack. O(1)


[Link]() Return true/false if the stack is empty or not. O(1)

Module 14 - Queue

Queue Built-in Functions:

Name Details Time


Complexity
[Link]() Add an element to the tail/back of the queue. O(1)

[Link]() Delete the first value of the queue. O(1)

[Link]() Access the first element of the queue. O(1)

[Link]() Returns the size of the queue. O(1)

[Link]() Return true/false if the queue is empty or not. O(1)

[Link]() Access the last element of the queue. O(1)


Topics:
1.​ Stack
2.​ Queue

Question: Take two stacks of size N and M as input and check if both of them are
the same or not. Don’t use STL stack to solve this problem.

Sample Input Sample Output


5 YES
10 20 30 40 50
5
10 20 30 40 50
5 NO
10 20 30 40 50
4
10 20 30 40
5 NO
10 20 30 40 50
5
50 40 30 20 10

Question: Take a stack of size N and a queue of size M as input. Then check if
both of them are the same or not in the order of removing. You should use STL to
solve this problem.
Sample Input Sample Output
5 NO
10 20 30 40 50
5
10 20 30 40 50
5 NO
10 20 30 40 50
4
10 20 30 40
5 YES
10 20 30 40 50
5
50 40 30 20 10
Question: Take a stack of size N as input and copy those elements to another stack
to get the values in the order they were inserted and print them. You should use
STL to solve this problem.

Sample Input Sample Output


5 10 20 30 40 50
10 20 30 40 50

Question: Take a queue of size N as input. You need to copy those elements in
another queue in reverse order. You might use stack here. You should use STL to
solve this problem. After copying in another queue, print the elements of that
queue.
Sample Input Sample Output
5 50 40 30 20 10
10 20 30 40 50

Tree

Binary Tree

N-ary Tree
Binary Tree -Level Order Traversal

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* left;
Node* right;

Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;

}
};

void level_order(Node* root)


{
queue<Node*> q;
[Link](root);
while(![Link]())
{
//1 ber kore ana - Eject from queue

Node* f = [Link]();
[Link]();

//2 oi node ke niye kaj

cout<<f->val<<" ";
//3 push the children

if(f->left != NULL)
{
[Link](f->left);
}

if(f->right != NULL)
{
[Link](f->right);
}

int main()
{
Node* root = new Node(10);
Node* a = new Node(20);
Node* b = new Node(30);
Node* c = new Node(40);
Node* d = new Node(50);
Node* e = new Node(60);

root->left = a;
root->right = b;
a->left = c;
b->left = d;
b->right = e;

level_order(root);
return 0;
}

Binary Tree Input

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* left;
Node* right;

Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;

}
};

Node* input_tree()
{
int val;
cin>>val;
Node* root ;
if(val == -1) root = NULL;
else root = new Node(val);
queue<Node*> q;
if (root) [Link](root);
while(![Link]())
{
//1 ber kore ana

Node* f = [Link]();
[Link]();

//2 oi Node niye kaj

int l,r;
cin>>l>>r;

Node* myLeft, *myRight;

if(l == -1) myLeft = NULL;


else myLeft = new Node(l);

if(r == -1) myRight = NULL;


else myRight = new Node(r);

f->left = myLeft;
f->right = myRight;

//3 push children

if(f->left)
{
[Link](f->left);
}
if(f->right)
{
[Link](f->right);
}

}
return root;
}

void level_order(Node* root)


{
if(root == NULL)
{
cout<<"No Tree";
return;
}

queue<Node*> q;
[Link](root);
while(![Link]())
{
//1 ber kore ana - Eject from queue

Node* f = [Link]();
[Link]();

//2 oi node ke niye kaj

cout<<f->val<<" ";

//3 push the children

if(f->left != NULL)
{
[Link](f->left);
}

if(f->right != NULL)
{
[Link](f->right);
}

int main()
{

Node* root = input_tree();

level_order(root);

return 0;
}

Count Nodes in Binary Tree


#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* left;
Node* right;

Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;

}
};

Node* input_tree()
{
int val;
cin>>val;
Node* root ;
if(val == -1) root = NULL;
else root = new Node(val);
queue<Node*> q;
if (root) [Link](root);
while(![Link]())
{
//1 ber kore ana

Node* f = [Link]();
[Link]();

//2 oi Node niye kaj

int l,r;
cin>>l>>r;

Node* myLeft, *myRight;

if(l == -1) myLeft = NULL;


else myLeft = new Node(l);

if(r == -1) myRight = NULL;


else myRight = new Node(r);

f->left = myLeft;
f->right = myRight;

//3 push children

if(f->left)
{
[Link](f->left);
}

if(f->right)
{
[Link](f->right);
}

}
return root;
}

void level_order(Node* root)


{
if(root == NULL)
{
cout<<"No Tree";
return;
}

int count = 0;

queue<Node*> q;
[Link](root);
while(![Link]())
{
//1 ber kore ana - Eject from queue

Node* f = [Link]();
[Link]();

//2 oi node ke niye kaj

cout<<f->val<<" ";
count++;

//3 push the children

if(f->left != NULL)
{
[Link](f->left);
}

if(f->right != NULL)
{
[Link](f->right);
}

}
cout<<endl;

cout<<"size : "<<count<<endl;

int main()
{

Node* root = input_tree();

level_order(root);

return 0;
}

With easy recursion

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* left;
Node* right;

Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;

}
};

Node* input_binary_tree()
{
int val;
cin>>val;
Node* root;
if(val == -1)
{
root = NULL;
}
else
{
root = new Node(val);
}

queue<Node*> q;

if(root != NULL)
{
[Link](root);
}

while(![Link]())
{
Node* p = [Link]() ;
[Link]();

int l,r;

cin>>l>>r;
Node * myLeft,*myRight;

if(l == -1) myLeft = NULL;


else myLeft = new Node(l);
if(r== -1) myRight = NULL;
else myRight = new Node(r);

p->left = myLeft;
p->right = myRight;

if(p->left)
{
[Link](myLeft);
}

if(p->right)
{
[Link](myRight);
}

return root;

int count_nodes(Node* root )


{
if(root == NULL)
{
return 0;
}
int l = count_nodes(root->left);
int r = count_nodes(root->right);
return l+r+1;

int main()
{
Node* root = input_binary_tree();

cout<<count_nodes(root)<<endl;

return 0;
}
Count Leaf Nodes In binary tree

#include<bits/stdc++.h>
using namespace std;

class Node
{
public:
int val;
Node* left;
Node* right;

Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;

}
};

Node* input_binary_tree()
{
int val;
cin>>val;
Node* root;
if(val == -1)
{
root = NULL;
}
else
{
root = new Node(val);
}

queue<Node*> q;

if(root != NULL)
{
[Link](root);
}

while(![Link]())
{
Node* p = [Link]() ;
[Link]();

int l,r;

cin>>l>>r;

Node * myLeft,*myRight;

if(l == -1) myLeft = NULL;


else myLeft = new Node(l);
if(r== -1) myRight = NULL;
else myRight = new Node(r);

p->left = myLeft;
p->right = myRight;

if(p->left)
{
[Link](myLeft);
}

if(p->right)
{
[Link](myRight);
}

return root;

int count_leaf_nodes(Node* root )


{
if(root == NULL)
{
return 0;
}

if(root->left == NULL && root->right == NULL)


return 1;

int l = count_leaf_nodes(root->left);
int r = count_leaf_nodes(root->right);
return l+r;

}
int main()
{
Node* root = input_binary_tree();

cout<<count_leaf_nodes(root)<<endl;

return 0;
}

Binary Tree Inputs

300 20 30 40 -1 50 60 -1 -1 -1 -1 -1 -1

10
20 30
40 70 -1 50
90 110 -1 -1 80 60
-1 -1 -1 -1 100 -1 -1 -1

-1 -1
*

18 7 21 -1 12 20 26 9 15 -1 -1 -1 -1 -1 -1 -1 -1

10 6 23 -1 9 19 29 7 -1 12 -1 -1 35 -1 -1 -1 -1 -1 -1
Map

#include<bits/stdc++.h>
using namespace std;

int main()
{
string s;
getline(cin,s);

stringstream ss(s);

string word;

map<string,int> mp;

while(ss >> word)


{
mp[word]++;
}

for(auto it = [Link](); it != [Link](); it++)


{
cout<<it->first<<" "<<it->second<<endl;
}

return 0;
}
[Link]-To-Algorithms
Graph

(Even though its a data structure it’s covered in algo section)

BFS

DFS

Number Of Components DFS

#include<bits/stdc++.h>
using namespace std;

//Graph Components

//inputs

// 8 6
// 1 2
// 0 5
// 2 3
// 6 7
// 4 5
// 1 3
//output 3

vector<int> adj_list[1005];
bool vis[1005];

void dfs(int src)


{
//no need for base case
// cout<<src<<" ";
vis[src] = true;
for(int child : adj_list[src] )
{
if(vis[child] == false)
{
dfs(child);
}
}

int main()
{
int n,e;
cin>>n>>e;

int count = 0;

while(e--)
{
int a,b;
cin>>a>>b;
adj_list[a].push_back(b);
adj_list[b].push_back(a);
}
memset(vis,false,sizeof(vis));

for (int i = 0; i < n; i++)


{
if(vis[i] == false )
{
dfs(i);
count++;

}
}

cout<<count<<endl;

return 0;
}

Bellman-Ford-Funtion

#include<bits/stdc++.h>
using namespace std;

//Directed Graph
// 4 4
// 0 2 5
// 0 3 12
// 2 1 2
// 1 3 3

// 4 4
// 0 1 5
// 1 2 3
// 2 3 2
// 3 1 -6

class Edge
{
public:
int a,b,c;
Edge(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};

int n,e;

int dis[1005];

vector<Edge> edge_list;

void bellman_ford()
{
for (int i = 0; i < n-1; i++) //o(v)
{
for(auto ed : edge_list) //O(E)
{
int a,b,c;

a= ed.a;
b = ed.b;
c = ed.c;
if(dis[a] != INT_MAX && dis[a] + c < dis[b])
{
dis[b] = dis[a] + c;
}

}
}

} //O(VE)

int main()
{

cin>>n>>e;

while(e--)
{
int a,b,c;
cin>>a>>b>>c;
edge_list.push_back(Edge(a,b,c));

for (int i = 0; i < n; i++)


{
dis[i] = INT_MAX;
}

dis[0] = 0;

bellman_ford();

for (int i = 0; i < n; i++)


{
cout<<i<<"-> "<<dis[i]<<endl;
}

// for(auto ed : edge_list)
// {
// cout<<ed.a<<" "<<ed.b<<" "<<ed.c<<endl;
// }
return 0;
}

//Space Complexity O(V) - dis[V]


//Final Time Complexity O(VE)

Bellman-Ford

#include<bits/stdc++.h>
using namespace std;

//directed graph
// 4 4
// 0 2 5
// 0 3 12
// 2 1 2
// 1 3 3

class Edge
{
public:
int a,b,c;
Edge(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};

int dis[1005];

int main()
{
int n,e;
cin>>n>>e;

vector<Edge> edge_list;

while(e--)
{
int a,b,c;
cin>>a>>b>>c;
edge_list.push_back(Edge(a,b,c));

}
for (int i = 0; i < n; i++)
{
dis[i] = INT_MAX;
}

dis[0] = 0;

for (int i = 0; i < n-1; i++)


{
for(auto ed : edge_list)
{
int a,b,c;

a= ed.a;
b = ed.b;
c = ed.c;
if(dis[a] != INT_MAX && dis[a] + c < dis[b])
{
dis[b] = dis[a] + c;
}

}
}

for (int i = 0; i < n; i++)


{
cout<<i<<"-> "<<dis[i]<<endl;
}

// for(auto ed : edge_list)
// {
// cout<<ed.a<<" "<<ed.b<<" "<<ed.c<<endl;
// }
return 0;
}

Detect Negative Weighted Cycle with Bellman-Ford


#include <bits/stdc++.h>
using namespace std;

// Directed Graph

// 4 4
// 0 2 5
// 0 3 12
// 2 1 2
// 1 3 3

// 4 4
// 0 1 5
// 1 2 3
// 2 3 2
// 3 1 -6

class Edge
{
public:
int a, b, c;
Edge(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};
int n, e;

int dis[1005];

vector<Edge> edge_list;

void bellman_ford()
{
for (int i = 0; i < n - 1; i++) // o(v)
{
for (auto ed : edge_list) // O(E)
{
int a, b, c;

a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != INT_MAX && dis[a] + c < dis[b])
{
dis[b] = dis[a] + c;
}
}
}

bool flag = false;

for (auto ed : edge_list) // O(E)


{
int a, b, c;

a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != INT_MAX && dis[a] + c < dis[b])
{
flag = true;
}
}

if (flag)
{
cout << "Negative Weighted Cycle Detected" << endl;
}
else
{
cout << "No Negative Weighted Cycle" << endl;
for (int i = 0; i < n; i++)
{
cout << i << "-> " << dis[i] << endl;
}
}

} // O(VE)

int main()
{

cin >> n >> e;

while (e--)
{
int a, b, c;
cin >> a >> b >> c;
edge_list.push_back(Edge(a, b, c));
}

for (int i = 0; i < n; i++)


{
dis[i] = INT_MAX;
}

dis[0] = 0;

bellman_ford();

// for(auto ed : edge_list)
// {
// cout<<ed.a<<" "<<ed.b<<" "<<ed.c<<endl;
// }
return 0;
}

// Space Complexity O(V) - dis[V]


// Final Time Complexity O(VE)

Module 14 : Basics of Dynamic Programming

Climbing Stairs [Easy]​


- Asked in [Amazon, Google, Apple, Facebook, Microsoft] ​
solution link​

class Solution {
public:
int climbStairs(int n) {

int fibo[50];

fibo[0] = 0;
fibo[1] = 1;
fibo[2] = 2;

for(int i = 3 ; i < n+1 ; i++)


{
fibo[i] = fibo[i-1] + fibo[i-2];
}

return fibo[n];

}
};

Visualizer link: [Link]
LeetCode-Algorithm Problems
1.​Keys and Rooms [Easy] ​
- Asked in [Google, Amazon] ​
solution link​

LeetCode Problem List:

1.​Island Perimeter [Easy] ​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

2.​Find if path exists in graph [Easy]​


- Asked in [Microsoft] ​
solution link​

3.​Max area of island [Medium]​


- Asked in [Google, Facebook, Amazon, Microsoft] ​
solution link​

4.​Number of islands [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft,
LinkedIn, Snapchat, Twitter, Uber] ​
solution link​

5.​Count sub islands [Medium]​


- Asked in [Twitter] ​
solution link​
6.​Number of closed islands [Medium]​
- Asked in [Google] ​
solution link

DFS Grid Solve

class Solution {
public:

bool vis[105][105];
vector<pair<int,int>> d = {{1,0},{-1,0},{0,-1},{0,1}};
int n,m,cnt;
bool flag = true;

bool valid(int i,int j)


{
if(i<0 || j<0 || i>=n || j>=m )
{
return false;
}
return true;
}
void dfs(int si,int sj, vector<vector<int>>& grid)
{
vis[si][sj] = true;

for(int i = 0 ; i < 4 ; i++)


{
int ci = si + d[i].first;
int cj = sj + d[i].second;
if(!valid(ci,cj))
{
flag = false;
}
if(valid(ci,cj) && !vis[ci][cj] && grid[ci][cj] ==
0)
{
dfs(ci,cj,grid);
}

int closedIsland(vector<vector<int>>& grid) {


n = [Link]();
m = grid[0].size();

memset(vis,false,sizeof(vis));

cnt = 0;
for(int i = 0 ; i<n ; i++)
{
for(int j = 0 ; j<m ; j++)
{
if(!vis[i][j] && grid[i][j] == 0 )
{

flag = true;
dfs(i,j,grid);
if(flag == true)
{
cnt++;
}

}
}

}
return cnt;
}
};

BFS Grid Solve

class Solution {
public:

bool vis[105][105];
vector<pair<int,int>> d = {{1,0},{-1,0},{0,-1},{0,1}};
int n,m,cnt;
bool flag = true;

bool valid(int i,int j)


{
if(i<0 || j<0 || i>=n || j>=m )
{
return false;
}
return true;
}

void bfs(int si,int sj, vector<vector<int>>& grid)


{
queue<pair<int,int>> q;
[Link]({si,sj});
vis[si][sj] = true;
while(![Link]())
{
pair<int,int> par = [Link]();
[Link]();
int par_i = [Link];
int par_j = [Link];
for(int i = 0 ; i<4 ; i++)
{
int ci = par_i + d[i].first;
int cj = par_j + d[i].second;
if(!valid(ci,cj))
{
flag = false;
}
if(valid(ci,cj) && !vis[ci][cj] &&
grid[ci][cj]==0 )
{
[Link]({ci,cj});
vis[ci][cj] = true;
}

int closedIsland(vector<vector<int>>& grid) {


n = [Link]();
m = grid[0].size();

memset(vis,false,sizeof(vis));

cnt = 0;

for(int i = 0 ; i<n ; i++)


{
for(int j = 0 ; j<m ; j++)
{
if(!vis[i][j] && grid[i][j] == 0 )
{

flag = true;
bfs(i,j,grid);
if(flag == true)
{
cnt++;
}
}
}

}
return cnt;
}
};
LeetCode Problem List:​

1.​Solve all the problems of today’s module with BFS and DFS both. ​

2.​Maximum number of fish in a grid [Medium]​


- Asked in [Adobe] ​
solution link​

3.​Surrounded Regions [Medium]​


- Asked in [Google, Facebook, Amazon] ​
solution link​

4.​Battleships in a Board [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

5.​Minimum Number of Vertices to Reach All Nodes ​


- Asked in [Google] ​
solution link​

6.​Shortest Path in Binary Matrix ​


- Asked in [Google, Facebook, Amazon] ​
solution link

প্রবলেমগুলো কমপ্লেক্স এবং হার্ড । টোটালি অপশনাল এগুলো। যদি আপনার হাতে সময় থাকে
এবং আরো প্র্যাকটিস করতে চান সেক্ষেত্রে ট্রাই করুন। নাহলে ইগনোর করুন।
Module 6.5: Practice Day 01
(GeeksforGeeks, CSES, LeetCode)

Topics:
1.​ Cycle Detection
2.​ BFS
3.​ DFS

Problem Links:

1.​Detect cycle in an undirected graph [GFG] - [Solve this using BFS]


2.​Detect cycle in an undirected graph [GFG] - [Solve this using DFS]​

3.​Course Schedule [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

4.​Find Eventual Safe States [Medium]​


- Asked in [Google] ​
solution link​

5.​Counting rooms [CSES]

XPSC Problem Links:

6.​A. Fashionable Array


7.​A. Dr. TC​



Extra Problems From Mod 5 & 6(Optional):​

8.​Maximum number of fish in a grid [Medium]​


- Asked in [Adobe] ​
solution link​

9.​Surrounded Regions [Medium]​


- Asked in [Google, Facebook, Amazon] ​
solution link​

10.​ Battleships in a Board [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

11.​ Minimum Number of Vertices to Reach All Nodes ​


- Asked in [Google] ​
solution link​
12.​ Shortest Path in Binary Matrix ​
- Asked in [Google, Facebook, Amazon] ​
solution link​

13.​ Number of Enclaves [Medium] ​


- Asked in [Google] ​
solution link​

14.​ Number of Provinces [Medium]​


- Asked in [Google, Facebook, Amazon] ​
solution link​

Module - 7 Extra Practice Problem

1.​Minimum Score of a Path Between Two Cities [Medium]​


- Asked in [Amazon] ​
solution link ​

2.​Map of Highest Peak [Medium]​


- Asked in [Google] ​
solution link​

3.​01 Matrix [Medium]​


- Asked in [Google, Amazon] ​
solution link
4.​
Module 3.5: Practice Day 02
(Leetcode and Practice)

Topics:
1.​ BFS
2.​ DFS
3.​ BFS, DFS on 2D Grid
4.​ Components

Problem Links:

1.​Counting Rooms [CSES]​

2.​Flood Fill [Easy] ​


- Asked in [Google, Amazon, Facebook, Apple, Microsoft] ​
solution link​

3.​Number of Closed Islands [Medium]​


- Asked in [Google]​
- [This problem is optional, Don’t look for support for this problem. We will solve
this problem on next module]

Module 7.5: Practice Day 02


(GFG, CSES, LeetCode)

Topics:
1.​ Dijkstra Algorithm
Problem Links:

1.​Implementing Dijkstra Algorithm [GFG]

class Solution {
public:

vector<pair<int,int>> adj[100005];

vector<int> dijkstra(int V, vector<vector<int>> &edges, int src) {

for (int i = 0; i < V; i++) adj[i].clear();

for (int i = 0; i < [Link](); i++)


{
int a = edges[i][0];
int b = edges[i][1];
int c = edges[i][2];
adj[a].push_back({b, c});
adj[b].push_back({a, c});

vector<int> dis(V, INT_MAX);

priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
[Link]({0,src});//O(logV)

dis[src] = 0;

while(![Link]()) //O(v)
{
pair<int,int> par = [Link]();
[Link]();//O(VlogV)

int par_dis = [Link];


int par_node = [Link];

for(auto child : adj[par_node]) //O(E)


{
int child_node = [Link];
int child_dis = [Link];
if(par_dis + child_dis < dis[child_node]) //Only different condition from
BFS
{
dis[child_node] = child_dis + par_dis;
[Link]({dis[child_node],child_node});//O(ElogV)
}

return dis;
}
};

2.​Building Roads [CSES]

#include<bits/stdc++.h>
using namespace std;

vector<int> adj[100005];
bool vis[100005];

void dfs(int src)


{
vis[src] = true;

for(int child : adj[src])


{
if(!vis[child])
{
dfs(child);
vis[child] = true;
}
}
}

int main()
{
int n,m;
cin>>n>>m;

vector<int> roads;

while(m--)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);

memset(vis,false,sizeof(vis));

for(int i = 1 ; i<=n ; i++)


{
if(vis[i] == false)
{
dfs(i);
roads.push_back(i);

int cnt = [Link]() - 1;

cout<<cnt<<endl;

for (int i = 0; i < (int)[Link]()-1; i++)


{
cout<<roads[i]<<" "<<roads[i+1]<<endl;
}

return 0;
}

3.​Network Delay Time [Medium]​


- Asked in [Amazon, Google] ​
solution link

class Solution {
public:

vector<pair<int,int>> adj[105];

int dis[105];

void dijkstra(int src)


{

priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<
int,int>>> pq;

[Link]({0,src});

dis[src] = 0;

while(![Link]())
{
pair<int,int> par = [Link]();
[Link]();
int par_dis = [Link];
int par_node = [Link];

for(auto child : adj[par_node])


{
int child_node = [Link];
int child_dis = [Link];
if(par_dis + child_dis < dis[child_node])
{
dis[child_node] = child_dis + par_dis;
[Link]({dis[child_node],child_node});
}

int networkDelayTime(vector<vector<int>>& times, int n, int


k) {

for (int i = 1; i <= n; i++) adj[i].clear();

for (int i = 0; i < [Link](); i++)


{
int a = times[i][0];
int b = times[i][1];
int c = times[i][2];
adj[a].push_back({b, c});

for (int i = 1; i <= n; i++)


{
dis[i] = INT_MAX;
}

dijkstra(k);

int ans = -1;

for(int i =1; i<= n ; i++)


{
if(dis[i] == INT_MAX )
{
return -1;
}
else
{
ans = max(dis[i],ans);
}

return ans;
}
};


Extra Practice Problems from Mod 7(Optional):

1.​Minimum Score of a Path Between Two Cities [Medium]​


- Asked in [Amazon] ​
solution link ​

2.​Map of Highest Peak [Medium]​


- Asked in [Google] ​
solution link​

3.​01 Matrix [Medium]​


- Asked in [Google, Amazon] ​
solution link​

Module 9 Extra Practice Problems


1.​Bellman-Ford [GFG]

// User function Template for C++

class Solution {
public:

class Edge
{
public:
int a,b,c;
Edge(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};

vector<int> bellmanFord(int V, vector<vector<int>>& edges, int src) {


// Code here
vector<Edge> edge_list;

for(auto ed : edges)
{
edge_list.push_back(Edge(ed[0],ed[1],ed[2]));
}

vector<int> dis(V);

for (int i = 0; i < V; i++)


{
dis[i] = 1e8;
}

dis[src] = 0;

for (int i = 0; i < V - 1; i++) // o(v)


{
for (auto ed : edge_list) // O(E)
{
int a, b, c;

a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != 1e8 && dis[a] + c < dis[b])
{
dis[b] = dis[a] + c;
}
}
}

for (auto ed : edge_list) // O(E)


{
int a, b, c;
a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != 1e8 && dis[a] + c < dis[b])
{
return { -1};
}

return dis;

}
};
2.​All Paths From Source to Target [Medium]​
- Asked in [Google, Amazon] ​
solution link​

class Solution {
public:

vector<vector<int>> nodes;

vector<int> v;

int n;

void dfs(int src,vector<vector<int>>& graph)


{

v.push_back(src);

if(src == n-1)
{
nodes.push_back(v);
}
else
{
for(int child : graph[src])
{

dfs(child,graph);

}
}
v.pop_back();

vector<vector<int>>
allPathsSourceTarget(vector<vector<int>>& graph) {

n = [Link]();

dfs(0,graph);

return nodes;

}
};
Climbing Stairs [Easy]DP​
- Asked in [Amazon, Google, Apple, Facebook, Microsoft] ​
solution link

Bottom Up DP​

class Solution {
public:
int climbStairs(int n) {

int fibo[50];

fibo[0] = 0;
fibo[1] = 1;
fibo[2] = 2;

for(int i = 3 ; i < n+1 ; i++)


{
fibo[i] = fibo[i-1] + fibo[i-2];
}

return fibo[n];

}
};

Recursion+DP

class Solution {
public:
int dp[50];

int fibo(int n)
{
if(n<3)
{
return n;
}
if(dp[n] != -1)
{
return dp[n];
}

dp[n] = fibo(n-1)+fibo(n-2);
return dp[n];

int climbStairs(int n) {

memset(dp,-1,sizeof(dp));

int ans = fibo(n);

return ans;

}
};
House-robber[Medium]
Solve

class Solution {
public:

int dp[105];

int recur(int idx,vector<int>& nums)


{
if(idx < 0)
{
return 0;
}

if(dp[idx] != -1 )
{
return dp[idx];
}

int op1 = nums[idx] + recur(idx - 2,nums);


int op2 = recur(idx - 1,nums);

dp[idx] = max(op1,op2);

return dp[idx];

int rob(vector<int>& nums) {

memset(dp,-1,sizeof(dp));

int n = [Link]();
return recur(n-1,nums);

}
};
Practice Modules
Introduction to Algorithms

Module 2.5: Practice Day 01


(GeeksforGeeks and Practice)

Topics:
1.​ Graph Representation
2.​ BFS

Problem Links:

1.​Keys and Rooms [Easy] ​


- Asked in [Google, Amazon] ​
solution link​

2.​Message Route - CSES


Question: You will be given an undirected graph as input. Then you will be given
a query Q. For each query, you will be given source S and destination D. You need
to print the shortest distance between S and D. If there is no path from S to D, print
-1.

Sample Input Sample Output


67 2
01
3
02
12 3
03
2
42
35 2
43
0
6
05
15
25
23
14
00
75 -1
01
-1
02
45 -1
46
57
3
04
51
13
Question: You will be given an undirected graph which will be connected as input.
Then you will be given a level L. You need to print the node values at level L in
descending order. The source will be 0 always.

Sample Input Sample Output


32 21
01
02
1
67 321
01
02
12
03
42
35
43
1
67 54
01
02
12
03
42
35
43
2
Question: You will be given an undirected graph as input. Then you will be given
a node N. You need to print the number of nodes that are directly connected to the
node N.

Sample Input Sample Output


65 2
01
02
03
23
45
2
65 3
01
02
03
23
45
0
77 3
01
12
23
13
40
05
56
1
Module 3.5: Practice Day 02
(Leetcode and Practice)

Topics:
1.​ BFS
2.​ DFS
3.​ BFS, DFS on 2D Grid
4.​ Components

Problem Links:
1.​Counting Rooms [CSES]​

2.​Flood Fill [Easy] ​


- Asked in [Google, Amazon, Facebook, Apple, Microsoft] ​
solution link​

3.​Number of Closed Islands [Medium]​


- Asked in [Google]​
- [This problem is optional, Don’t look for support for this problem. We will solve
this problem on next module]

Question: You will be given an undirected graph as input. Then you will be given
a node N. You need to tell the number of nodes that can be visited from node N.

Sample Input Sample Output


65 4
01
02
03
23
45
2
65 2
01
02
03
23
45
4
76 5
01
12
23
13
40
56
1
Question: You will be given an undirected graph as input. You need to tell the
number of components in this graph.

Sample Input Sample Output


65 2
01
02
03
23
45
97 3
01
02
03
23
45
68
76
77 1
01
12
23
13
40
05
56
10 5 6
12
(Because 7 8 and 9 nodes are
23
13 not connected, but they are also
40
components)
56
Question: You will be given an undirected graph as input. You need to tell the
number of nodes in each component in ascending order.

Sample Input Sample Output


65 24
01
02
03
23
45
97 234
01
02
03
23
45
68
76
77 7
01
12
23
13
40
05
56
10 5 111223
12
23
13 (Because 7 8 and 9 nodes are
40
not connected, but they are also
56
components)
Module 6.5: Practice Day 01
(GeeksforGeeks, CSES, LeetCode)

Topics:
1.​ Cycle Detection
2.​ BFS
3.​ DFS

Problem Links:

1.​Detect cycle in an undirected graph [GFG] - [Solve this using BFS]


2.​Detect cycle in an undirected graph [GFG] - [Solve this using DFS]​

3.​Course Schedule [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

4.​Find Eventual Safe States [Medium]​


- Asked in [Google] ​
solution link​

5.​Counting rooms [CSES]

XPSC Problem Links:

6.​A. Fashionable Array


7.​A. Dr. TC​



Extra Problems From Mod 5 & 6(Optional):​

8.​Maximum number of fish in a grid [Medium]​


- Asked in [Adobe] ​
solution link​

9.​Surrounded Regions [Medium]​


- Asked in [Google, Facebook, Amazon] ​
solution link​

10.​ Battleships in a Board [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

11.​ Minimum Number of Vertices to Reach All Nodes ​


- Asked in [Google] ​
solution link​
12.​ Shortest Path in Binary Matrix ​
- Asked in [Google, Facebook, Amazon] ​
solution link​

13.​ Number of Enclaves [Medium] ​


- Asked in [Google] ​
solution link​

14.​ Number of Provinces [Medium]​


- Asked in [Google, Facebook, Amazon] ​
solution link​

Module - 7 Extra Practice Problem

1.​Minimum Score of a Path Between Two Cities [Medium]​


- Asked in [Amazon] ​
solution link ​

2.​Map of Highest Peak [Medium]​


- Asked in [Google] ​
solution link​

3.​01 Matrix [Medium]​


- Asked in [Google, Amazon] ​
solution link
4.​
Module 7.5: Practice Day 02
(GFG, CSES, LeetCode)

Topics:
1.​ Dijkstra Algorithm
Problem Links:

1.​Implementing Dijkstra Algorithm [GFG]​

2.​Building Roads [CSES]​

3.​Network Delay Time [Medium]​


- Asked in [Amazon, Google] ​
solution link


Extra Practice Problems from Mod 7(Optional):

1.​Minimum Score of a Path Between Two Cities [Medium]​


- Asked in [Amazon] ​
solution link ​

2.​Map of Highest Peak [Medium]​


- Asked in [Google] ​
solution link​

3.​01 Matrix [Medium]​


- Asked in [Google, Amazon] ​
solution link
Module 10.5: Practice Day 1
(GFG,Codeforces)

Topics:
1.​ Bellman Ford Algorithm
2.​ Floyd Warshall Algorithm
3.​ Dijkstra Algorithm

Problem Links:

1.​Bellman-Ford
2.​Floyd-Warshall
3.​Dijkstra? [Optional - just try it. Don’t look for support, we will solve it in
next module]​

4.​All Paths From Source to Target [Medium]​


- Asked in [Google, Amazon] ​
solution link
Module 11.5: Practice Day 02
(GFG, LeetCode, CSES, Codeforces)

Topics:
1.​ DSU

Problem Links:

1.​ Disjoint set (Union-Find) [GFG]

/*Complete the functions below*/


int find(int par[], int x) {
// add code here
if(par[x] == x )
{
return x;
}
else
{
int parent = find(par,par[x]);
par[x] = parent;
return parent;
}
}
void unionSet(int par[], int x, int z) {
int leader1 = find(par,x);
int leader2 = find(par,z);

par[leader1] = leader2;

}
2.​ Building Roads [CSES] [Try to solve this using DSU]
3.​ Roads not only in Berland [Codeforces] (Optional. Read this tutorial -
[Link] )

[Link] - Number Or Enclaves (Medium)

class Solution {
public:

bool vis[505][505];
vector<pair<int,int>> d = {{0,1},{0,-1},{1,0},{-1,0}};
int n,m;
bool flag;
int cnt;

bool valid(int i,int j)


{
if(i<0 || j<0 || i>=n || j>=m)
{
return false;
}
return true;
}

void dfs(int si,int sj,vector<vector<int>>& grid)


{
vis[si][sj] = true;
cnt++;
for(int i=0;i<4;i++)
{
int ci = si + d[i].first;
int cj = sj + d[i].second;

if(!valid(ci,cj))
{
flag = false;

}
else if(!vis[ci][cj] && grid[ci][cj] == 1)
{
dfs(ci,cj,grid);
}

int numEnclaves(vector<vector<int>>& grid) {


memset(vis,false,sizeof(vis));

n = [Link]();
m = grid[0].size();
int ans = 0;

for(int i = 0 ; i < n ; i++)


{
for(int j = 0 ; j < m ; j++)
{
if(!vis[i][j] && grid[i][j] == 1)
{
cnt = 0, flag = true;
dfs(i,j,grid);
if(flag)
{
ans = ans + cnt;
}
}
}
}

return ans;
}
};

Module 13 problem Sets

1.​ Road Construction [CSES]


2.​ Roads not only in Berland [Codeforces]
3.​ Dijkstra? [Codeforces]
4.​ Sundorban [Outsbook]
Module 14.5: Practice Day 1
(CF, Leetcode links)

Topics:
1.​ Dynamic Programming

Problem links:
1.​Print Digits using Recursion
2.​Factorial
3.​Reach Value
4.​Fibonacci Number [Easy]​
- Asked in [Facebook, Amazon, Apple, Microsoft] ​
solution link​

Bottom-Up-DP(Loop)
class Solution {
public:
int fib(int n) {

int fibo[50];

fibo[0] = 0;
fibo[1] = 1;

for(int i = 2 ; i < n+1 ; i++)


{
fibo[i] = fibo[i-1] + fibo[i-2];
}

return fibo[n];

}
};

Top-Down-Recursive-DP-Memoization

class Solution {
public:
int dp[50];

int f(int n)
{
if(n<2)
{
return n;
}
if(dp[n] != -1)
{
return dp[n];
}

dp[n] = f(n-1) + f(n-2);


return dp[n];
}

int fib(int n) {

memset(dp,-1,sizeof(dp));

int ans = f(n);


return ans;

}
};

Top-Down-DP-Memoization-Recursive

class Solution {
public:

int dp[50];
int f(int n)
{
if(n<2)
{
return n;
}
if(n == 2)
{
return 1;
}
if(dp[n] != -1)
{
return dp[n];
}

dp[n] = f(n-1) + f(n-2) + f(n-3);

return dp[n];
}

int tribonacci(int n) {
memset(dp,-1,sizeof(dp));

int ans = f(n);


return ans;
}
};

5.​N-th Tribonacci Number [Easy]​


solution link

Bottom Up DP - Loop
class Solution {
public:
int tribonacci(int n) {

int fibo[50];

fibo[0] = 0;
fibo[1] = 1;
fibo[2] = 1;

for(int i = 3 ; i < n+1 ; i++)


{
fibo[i] = fibo[i-1] + fibo[i-2] + fibo[i-3];
}

return fibo[n];

}
};

Extra Problem links:​

6.​Nearest Exit from Entrance in Maze [Medium]​


- Asked in [Google, Amazon] ​
solution link​

7.​Shortest Bridge [Medium]​


- Asked in [Google, Microsoft]​
solution link

Module 15.5: Practice Day 02


(CF, Leetcode)
Topics:
1.​ 0-1 Knapsack
2.​ Making Choices

Problem links:

1.​U. Knapsack
#include <bits/stdc++.h>
using namespace std;

//CodeForces

int val[1005], wieght[1005];

int dp[1005][1005];

int knapsack(int i, int mx_weight)


{
if (i < 0)
{
return 0;
}
if (mx_weight <= 0)
{
return 0;
}

if (dp[i][mx_weight] != -1)
return dp[i][mx_weight];

if (wieght[i] <= mx_weight)


{

int op1 = knapsack(i - 1, mx_weight - wieght[i]) + val[i];


int op2 = knapsack(i - 1, mx_weight);
dp[i][mx_weight] = max(op1, op2);
return dp[i][mx_weight];
}
else
{

int op2 = knapsack(i - 1, mx_weight);


dp[i][mx_weight] = op2;
return dp[i][mx_weight];
}
}

int main()
{
int n, mx_weight;

cin >> n >> mx_weight;

for (int i = 0; i < n; i++)


{
cin >> wieght[i];
cin >> val[i];
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= mx_weight; j++)
{
dp[i][j] = -1;
}
}

cout << knapsack(n - 1, mx_weight) << endl;

return 0;
}

2.​X. The maximum path-sum

3.​Minimum Path Sum [Medium]​


- Asked in [Amazon, Google, Apple, Microsoft] ​
solution link

class Solution {
public:
int dp[205][205];
int n,m;

int rec(int i,int j,vector<vector<int>>& grid)


{

if(i>=n || j>=m)
return INT_MAX;
if(i == n-1 && j == m-1)
return grid[i][j];
if(dp[i][j] != -1 )
return dp[i][j];

int op1 = rec(i,j+1,grid);

int op2 = rec(i+1,j,grid);


dp[i][j] = grid[i][j] + min(op1,op2);
return dp[i][j];

int minPathSum(vector<vector<int>>& grid) {

n = [Link]();
m = grid[0].size();
memset(dp,-1,sizeof(dp));

return rec(0,0,grid);

}
};

4.​Min Cost Climbing Stairs [Easy]​


- Asked in [Amazon] ​
solution link​

5.​House Robber [Medium]​


- Asked in [Google, Amazon, Microsoft, Apple] ​
solution link

class Solution {
public:

int dp[105];

int recur(int idx,vector<int>& nums)


{
if(idx < 0)
{
return 0;
}
if(dp[idx] != -1 )
{
return dp[idx];
}

int op1 = nums[idx] + recur(idx - 2,nums);


int op2 = recur(idx - 1,nums);

dp[idx] = max(op1,op2);

return dp[idx];

int rob(vector<int>& nums) {

memset(dp,-1,sizeof(dp));

int n = [Link]();

return recur(n-1,nums);

}
};

6.​House Robber II [Medium] [Optional]​


- Asked in [Google, Microsoft] ​
solution link

class Solution {
public:
int dp[105];
int rec(int idx,vector<int>& nums)
{
if(idx < 0)
{
return 0;
}
if(dp[idx] != -1)
{
return dp[idx];
}

int op1 = nums[idx] + rec(idx-2,nums);


int op2 = rec(idx-1,nums);

dp[idx] = max(op1,op2);

return dp[idx];
}

int rob(vector<int>& nums) {


int n = [Link]();

if(n == 1)
{
return nums[0];
}

memset(dp,-1,sizeof(dp));
int ans1 = rec([Link]()-2,nums);

memset(dp,-1,sizeof(dp));
[Link]([Link]());
int ans2 = rec([Link]()-1,nums);
return max(ans1,ans2);

}
};

7.​Pascal's Triangle [Easy]​


- Asked in [Google, Facebook, Amazon, Microsoft, Apple] ​
solution link​
LeetCode-DS Problems
Module 11-Problem solving

1.​ Middle of the Linked List [Easy]​


- Asked in [Amazon, Apple]
class Solution {
public:

int size_of_linked_list(ListNode* head)


{
ListNode* temp = head;
int count = 0;
while(temp != NULL)
{
count++;
temp = temp->next;
}
return count;
}
ListNode* middleNode(ListNode* head) {

ListNode* temp = head;


int size = size_of_linked_list(head);
for(int i = 1; i<=size/2 ; i++)
{
temp = temp->next;
}
return temp;

}
};

2.​ Linked List Cycle [Easy]​


- Asked in [Facebook, Amazon, Apple, Microsoft, Google, Samsung,
LinkedIn, Uber]​

class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
bool flag = false;
while(fast != NULL && fast->next != NULL )
{
slow = slow->next;
fast = fast->next->next;
if(slow==fast)
{
flag = true;
break;
}

}
return flag;
}
};
3.​ Remove Duplicates from Sorted List [Easy]​
- Asked in [Goole, Amazon, Apple, Microsoft, Uber]

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* temp = head;
if(head==NULL)
{
return head;
}
while(temp->next != NULL)
{
if(temp->val == temp->next->val)
{
temp->next = temp->next->next;
}
else
{
temp = temp->next;
}
}
return head;
}
};

4.​ Reverse Linked List [Easy]​


- Asked in [Facebook, Amazon, Microsoft, Apple, Google, Uber, Nvidia,
Adobe, Twitter, Snapchat, Paypal]

class Solution {
public:

void reverse_recursion(ListNode* &head,ListNode* temp)


{
if(temp->next == NULL )
{
head = temp ;
return;
}
reverse_recursion(head,temp->next);
temp->next->next = temp;
temp->next = NULL;

ListNode* reverseList(ListNode* head) {


if(head==NULL)
{
return head;
}
reverse_recursion(head,head);
return head;

}
};

5.​ Palindrome Linked List [Easy]​


- Asked in [Facebook, Amazon, Microsoft, Apple, Google, Snapchat,
Adobe, Twitter, Intel]

Solve With linked list

class Solution {
public:

void insert_at_tail(ListNode* &head,ListNode* &tail,int val)


{
ListNode* newnode = new ListNode(val);
if(head == NULL)
{
head = newnode;
tail = newnode;
return;
}
tail->next = newnode;
tail = newnode;
}

void reverse_recursion(ListNode* &head,ListNode* temp)


{
if(temp->next == NULL )
{
head = temp ;
return;
}
reverse_recursion(head,temp->next);
temp->next->next = temp;
temp->next = NULL;

bool isPalindrome(ListNode* head) {


ListNode* newhead = NULL;
ListNode* newtail = NULL;

ListNode* temp = head;

while(temp != NULL )
{
insert_at_tail(newhead,newtail,temp->val);
temp = temp->next;
}
reverse_recursion(newhead,newhead);

temp = head;
ListNode* temp2 = newhead;
while(temp!=NULL)
{
if(temp->val != temp2->val )
{
return false;
}
temp = temp->next;
temp2= temp2->next;
}

return true;

}
};

Short Solve with Vector or STL List

class Solution {
public:

bool isPalindrome(ListNode* head) {

vector<int> v;
ListNode* temp = head;

while(temp != NULL )
{
v.push_back(temp->val);
temp = temp->next;
}

vector<int> v2;

v2 = v;

reverse([Link](),[Link]());

if(v != v2)
{
return false;
}

return true;

}
};

6.​ Delete Node in a Linked List [Medium]​


- Asked in [Apple, Amazon, Microsoft, Google, Adobe, Paypal]

class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
Module 11.5: Practice Day 02
(Leetcode Links)

Topics:
1.​ Singly Linked List
1.​ Remove Linked List Elements [Easy]​
- Asked in [Facebook, Amazon, Microsoft]​
solution explained with animation​

2.​ Remove Nth Node From End of List [Medium] ​


- Asked in [Google, Facebook, Amazon, Microsoft, Apple] ​
solution explained with animation ​
3.​ Intersection of Two Linked Lists [Easy]​
- Asked in [Facebook, Amazon, Microsoft]​
solution explained with animation​

4.​ Swapping Nodes in a Linked List [Medium]


- Asked in [Google, Facebook, Amazon, Microsoft]​

class Solution {
public:
int size_singly_linked_list(ListNode* head)
{
ListNode* temp = head;
int count = 0;
while(temp != NULL)
{
temp = temp->next;
count++;
}
return count ;
}

ListNode* swapNodes(ListNode* head, int k) {

ListNode* first = head;


ListNode* last = head;

int size = size_singly_linked_list(head);

int fcount = 1;
int lcount = 1;

while (fcount < k)


{
first = first->next;
fcount++;
}

while (lcount < (size-k)+1 )


{
last = last->next;
lcount++;
}

swap(first->val,last->val);

return head;

};
5.​ Merge Nodes in Between Zeros [Medium]​
- Asked in [Google, Facebook, Amazon, Microsoft]​

6.​ Delete the Middle Node of a Linked List [Medium]​


- Asked in [Amazon, Microsoft, Adobe]​

Phitron’s Leetcode Interview preparation playlist on Linked


List - by Mahmud Hossain Pias: Link


Module-13-Extra Problems

1.​ Maximum Nesting Depth of the Parentheses [Easy] ​


- Asked in [Intel] ​

2.​ Minimum String Length After Removing Substrings


[Easy] ​

এক্সট্রা প্র্যাকটিস প্রবলেম গুলো অপশনাল হিসেবে দেওয়া হয়। সবার করাটা বাধ্যতামূলক নয়। না
পারলে টেনশন এর কিছু নেই। আমরা এখনো মডিউলে স্ট্যাক নিয়ে প্রবলেম সলভিং দেখি নাই। এই
উইকের লাস্ট মডিউলে আমরা দেখব। তখন ইজিলি হয়ে যাবে।​
তারপরও উপরের দুটি প্রবলেম এর সল্যুশন লিংক দিয়ে দিচ্ছি। ​
আগে নিজে ট্রাই করে তারপর দেখে নিতে পারেন। বাট কারো থেকে হেল্প নিয়ে জোড় করে করতে হবে
না। সামনে স্ট্যাক নিয়ে প্রবলেম সল্ভিং মডিউল আসলে তখন পেরে যাবেন। ​

1.​ Maximum Nesting Depth of the Parentheses Solution​

2.​ Minimum String Length After Removing Substrings Solution


Module 14 Extra Problems

1.​ Implement Stack using Queues [Easy]


2.​ Implement Queue using Stacks [Easy]

conceptual DS 4-1
1.​Baseball Game [Easy] ​
- Asked in [Amazon] ​
solution link​

2.​Time Needed to Buy Tickets [Easy] ​


- Asked in [Google, Amazon, Facebook] ​
solution link​

3.​Number of Students Unable to Eat Lunch [Easy] ​


- Asked in [Amazon, Apple] ​
solution link

Module 15
1.​Valid Parentheses [Easy] ​
- Asked in [Google, Amazon, Apple, Facebook, Microsoft, Adobe,
Samsung, IBM, Intel, Uber, Linkedin] ​
solution link​

2.​Backspace String Compare [Easy] ​


- Asked in [Google, Facebook] ​
solution link​
3.​Insert An Element At Its Bottom In A Given Stack -
Coding Ninjas​

#include <bits/stdc++.h>
stack<int> pushAtBottom(stack<int>& myStack, int x)
{
stack<int> st;

while(![Link]())
{
[Link]([Link]());
[Link]();
}

[Link](x);

while(![Link]())
{
[Link]([Link]());
[Link]();
}

return myStack;

}
4.​Maximum Equal Stack Sum - Coding Ninjas​

5.​Reversing a Queue - Coding Ninjas​

6.​Min Stack [Medium] ​


- Asked in [Google, Amazon, Apple, Microsoft, Adobe] ​
solution link
Extra Practice Problem

1.​ Remove all adjacent duplicates in string [Easy] ​


- Asked in [Google, Amazon] ​
solution link​

2.​ Make The String Great [Easy] ​


- Asked in [Google] ​
solution link​

3.​ Crawler Log Folder [Easy] ​


solution link
Module 15.5: Practice Day 02
(Problem Links)

Topics:
1.​ Stack
2.​ Queue
Problem Links:

1.​Implement Stack With Linked List - Coding Ninjas


2.​Implement a Queue - Coding Ninjas
3.​Reverse First K elements of Queue - Coding Ninjas
4.​Min Stack - Coding Ninjas
5.​Kevin’s stack problem - Coding Ninjas

Try these if you haven’t yet: Mod 15 extra practice problems


Write preorder, postorder and inorder traversal of this binary tree.

Answer: ​
Preorder = 1,7,2,6,5,11,9,9,5​
Inorder = 2,7,5,6,11,1,9,5,9 ​
Postorder = 2,5,11,6,7,5,9,9,1
Module 18.5: Practice Day 01
(Problem Links)

Topics:
1.​ Binary Tree
Problem Links:
1.​Postorder Traversal [Easy] ​
- Asked in [Google, Amazon, Facebook, Adobe] ​
solution link​

2.​Preorder Traversal [Easy] ​


- Asked in [Google, Amazon] ​
solution link​

3.​Inorder Traversal [Easy] ​


- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link​

4.​Sum of Left Leaves [Easy] ​


- Asked in [Google, Amazon, Facebook, Adobe] ​
solution link​

5.​Maximum Depth of Binary Tree [Easy] ​


- Asked in [Google, Amazon, Facebook, Apple, Microsoft] ​
solution link​

6.​Level Order Traversal - Coding Ninjas​

7.​Count Leaf Nodes - Coding Ninjas

Module 19 - Solved Problems from CodeNinjas (Problems in Codeninjas tab)

Extra Practice Problems

1.​ Binary Tree Right Side View [Medium] ​


- Asked in [Microsoft, Facebook, Apple, Amazon, Adobe] ​
solution link

2.​Invert Binary Tree [Easy]​


- Asked in [Google, Microsoft, Facebook, Amazon] ​
solution link​

3.​Diameter of Binary Tree [Easy] ​


- Asked in [Google, Microsoft, Facebook, Apple, Amazon,
Adobe] ​
solution link

Module 21-Extra Practice Problems

1.​Convert Sorted Array to Binary Search Tree [Easy] ​


- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link​

2.​Search in a Binary Search Tree [Easy] ​


- Asked in [Google, Adobe, IBM] ​
solution link
Module- 23-Extra Practice Problem

1.​Contains Duplicate [Easy] ​


- Asked in [Amazon, Microsoft, Apple, Adobe] ​
solution link​

Solve 1

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {

map<int,int> mp;

for(int x: nums)
{
mp[x]++;
}

for(auto it = [Link]() ; it != [Link]() ; it++)


{
if(it->second >= 2 )
{
return true;
}

return false;

}
};

Solve 2

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {

set<int> s;

for(int x: nums)
{
[Link](x);
}

return [Link]() != [Link]();

}
};
2.​Valid Anagram [Easy] ​
- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link
Module 23.5: Practice Day 02
(Leetcode Links)

Topics:
1.​ Priority Queue
2.​ Set
3.​ Map
4.​ Recap of Linkedlist, Stack, Tree

Problem Links:
1.​Kth Largest Element in an Array [Medium] ​
- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link

Solve
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {

priority_queue<int,vector<int>,greater<int>> minheap;

for(int x: nums)
{
[Link](x);
if([Link]() > k )
{
[Link]();
}
}

return [Link]();

}
};

2.​Last Stone Weight [Easy] ​


- Asked in [Amazon] ​
solution link​

class Solution {
public:
int lastStoneWeight(vector<int>& stones) {

priority_queue<int> maxheap;

for(int x : stones)
{
[Link](x);
}

while(![Link]() )
{
int top1 = [Link]();
[Link]();
if([Link]())
{
return top1;
}
int top2 = [Link]();
[Link]();

int top = top1 - top2;


if(top != 0)
{
[Link](top);
}
}

if([Link]())
{
return 0;
}

return [Link]();

}
};

Solve 2

class Solution {
public:
int lastStoneWeight(vector<int>& stones) {
priority_queue<int> pq;
for(int x : stones)
[Link](x);
while(![Link]())
{
int first = [Link]();
[Link]();
if([Link]()) return first;
int sec = [Link]();
[Link]();
if(first != sec)
[Link](first - sec);
}
return 0;
}
};
3.​Contains Duplicate [Easy] ​
- Asked in [Amazon, Microsoft, Apple, Adobe] ​
solution link

4.​Valid Anagram [Easy] ​


- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link

Recap Problems:

1.​ Middle of the Linked List [Easy]​


- Asked in [Amazon, Apple]​
solution link​

2.​ Merge Nodes in Between Zeros [Medium]​


- Asked in [Google, Facebook, Amazon, Microsoft]​
solution explained with animation​
solution link​

3.​ Valid Parentheses [Easy] ​


- Asked in [Google, Amazon, Apple, Facebook, Microsoft] ​
solution link​

4.​ Inorder Traversal [Easy] ​


- Asked in [Google, Amazon, Facebook, Microsoft, Apple] ​
solution link​

5.​ Maximum Depth of Binary Tree [Easy] ​


- Asked in [Google, Amazon, Facebook, Apple, Microsoft] ​
solution link
CodeForces-Solves
CodeForces-Solves

#include<bits/stdc++.h>
using namespace std;

//CodeForces-Sheet-2-Loops-Z-Three-Numbers

int main()
{
int k,s;
cin>>k>>s;

int x=0;
int y=0;
int z=0;
int count = 0;

for(x = 0; x<=k ; x++ )


{
for(y = 0; y<=k ; y++ )
{
z = s-x-y;
if(z>=0 && z<=k)
{
count++;
// cout<<y<<" "<<y<<" "<<z<<" "<<endl;

}
}
}

cout<<count;
return 0;
}

N-Shift Zeros

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

int arr[n];

for(int i = 0 ; i < n ; i++)


{
cin>>arr[i];
}

for (int i = 0; i < n; i++)


{
for (int j = i+1; j < n; j++)
{
if(arr[i] == 0){
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for(int i = 0 ; i < n ; i++)
{
cout<<arr[i]<<" ";
}

return 0;
}

Solve 2

#include<bits/stdc++.h>
using namespace std;

void shift_zero(int n)
{
int a[n];

for(int i = 0 ; i < n ; i++)


{
cin>>a[i];
}

int idx = 0;

for(int i = 0 ; i < n ; i++)


{
if(a[i]!=0)
{

int temp = a[i];


a[i] = a[idx];
a[idx] = temp;
idx++;
}
}

for(int i = 0 ; i < n ; i++)


{
cout<<a[i]<<" ";
}
}

int main()
{
int n;
cin>>n;

shift_zero(n);

return 0;
}

C-Choose Elements

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;

long long int sumnum;

cin>> sumnum;

vector<long long int> a(n);

for(int i = 0; i < n ; i++ )


{
cin>>a[i];
}

sort([Link](), [Link](), greater<int>());

long long int sum = 0;

int count = 0;
for (int i = 0; i < n && count < sumnum; i++)
{
if (a[i] > 0)
{
sum =sum + a[i];
count++;
}
else
{
break;
}
}

cout<<sum<<endl;

return 0;
}
Roads not only in Berland [Codeforces-Algorithm Problem]

#include<bits/stdc++.h>
using namespace std;

//input

// 7
// 1 2
// 2 3
// 3 1
// 4 5
// 5 6
// 6 7

//output

// 1
// 3 1 3 7

int par[1005];
int group_size[1005];

int find(int node)


{
if(par[node] == -1)
{
return node;
}
else
{
int parent = find(par[node]);
par[node] = parent;
return parent;
}
}

void dsu_union(int node1,int node2)


{
int leader1 = find(node1);
int leader2 = find(node2);

if(leader1 == leader2)
{
return;
}

if(group_size[leader1] >= group_size[leader2] )


{
par[leader2] = leader1;
group_size[leader1] += group_size[leader2];

}
else
{
par[leader1] = leader2;
group_size[leader2] += group_size[leader1];

int main()
{
int n;
cin>>n;
for (int i = 0; i < 1005; i++)
{
par[i] = -1;
group_size[i] = 1;
}

vector<pair<int,int>> rmv;
vector<pair<int,int>> create;

for (int i = 0; i < n-1; i++)


{

int a,b;
cin>>a>>b;

int leaderA = find(a);


int leaderB = find(b);

if(leaderA == leaderB)
{
rmv.push_back({a,b});

}
else
{
dsu_union(a,b);

}
}
for (int i = 2; i <=n; i++)
{
int leader_1 = find(1);
int leader_node = find(i);
if(leader_1 != leader_node)
{
create.push_back({1,i});
dsu_union(1,i);
}

cout<<[Link]()<<endl;

for (int i = 0; i < [Link](); i++)


{
cout<<rmv[i].first<<" "<<rmv[i].second<<" "<<create[i].first<<"
"<<create[i].second<<endl;
}

return 0;
}
Dijkstra? [Codeforces-Algorithm Problem]

#include<bits/stdc++.h>
using namespace std;

//input

// 5 6
// 1 2 2
// 2 5 5
// 2 3 4
// 1 4 1
// 4 3 3
// 3 5

//output

// 1 4 3 5

#define ll long long int

vector<pair<ll,ll>> adj_list[1000006];
ll dis[1000006];

ll parent[1000005];

void dijkstra(ll src)


{

priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>
pq;
dis[src] = 0;
[Link]({0,src});

while(![Link]())
{
pair<ll,ll> par = [Link]();
[Link]();

ll par_node = [Link];
ll par_dis = [Link];
for(auto child : adj_list[par_node])
{
ll child_node = [Link];
ll child_dis = [Link];

if(par_dis + child_dis < dis[child_node] )


{
dis[child_node] = par_dis + child_dis;

[Link]({dis[child_node],child_node});

parent[child_node] = par_node;
}

}
}
int main()
{
ll n,m;
cin>>n>>m;

for (ll i = 1; i <= n; i++)


{
dis[i] = LLONG_MAX;
parent[i] = -1;

while(m--)
{
ll a,b,c;
cin>>a>>b>>c;

adj_list[a].push_back({b,c});
adj_list[b].push_back({a,c});

dijkstra(1);

if(dis[n] == LLONG_MAX )
{
cout<< -1 <<endl;
}
else
{
ll node = n;
vector<ll>path;
while( node != -1)
{
path.push_back(node);
node = parent[node];

}
reverse([Link](),[Link]());
for(auto val : path)
{
cout<<val<<" ";
}
cout<<endl;
}

return 0;
}

Knapsack [Codeforces]
Sheet#4(Strings)
Extra Practice - Module - 5 - C++

A. Way Too Long Words​

A. Anton and Danik

Module 7.5 - C++

G. Even Hate Odd

H. N Times
A. Create A New String

Solve 1(Kinda Retarded)

​ #include<bits/stdc++.h>
​ using namespace std;

​ int main()
​ {
​ string s;
​ getline(cin,s);
​ string t;
​ getline(cin,t);

​ cout<<[Link]()<<" "<<[Link]()<<endl;

​ [Link]([Link]()," ");
​ [Link]([Link](),t);



​ cout<<s<<endl;



​ return 0;
​ }

Solve 2 (not so retarded)

#include<bits/stdc++.h>
using namespace std;

int main()
{
string s1,s2;

cin>>s1>>s2;

int size1 = [Link]();


int size2 = [Link]();

cout<<size1<<" "<<size2<<endl;
cout<<s1<<" "<<s2<<endl;

return 0;
}

C. Compare

Solve

#include<bits/stdc++.h>
using namespace std;

int main()
{
string x,y;
cin>>x>>y;

if(x<=y)
{
cout<<x;
}
else
{
cout<<y;
}
return 0;
}

G. Conversion

#include <bits/stdc++.h>
using namespace std;

int main()
{
string s;

cin >> s;

int size = [Link]();

for (int i = 0; i < size; i++)


{
if (s[i] == ',')
{
s[i] = ' ';
}
if ('a' <= s[i] && s[i] <= 'z')
{
s[i] = s[i] - 32;
}
else if ('A' <= s[i] && s[i] <= 'Z')
{
s[i] = s[i] + 32;
}
}

cout<<s<<'\n';

return 0;
}

C solve

​ #include <stdio.h>
​ #include <string.h>

​ int main()
​ {

​ char s[100001];
​ scanf("%s",s);
​ int size = strlen(s);
​ for(int i = 0; i<size; i++)
​ {
​ if(s[i]==',')
​ {
​ s[i]= ' ';
​ }
​ if( 'a'<=s[i] && s[i]<='z')
​ {
​ s[i]= s[i]-32;
​ }
​ else if( 'A'<=s[i] && s[i]<='Z')
​ {
​ s[i]= s[i]+32;
​ }
​ }

​ printf("%s",s);

​ return 0;
​ }

H. Good or Bad

​ #include<bits/stdc++.h>
​ using namespace std;


​ //CodeForces_Sheet_4_Strings

​ int main()
​ {
​ int t;
​ cin>>t;
​ for (int i = 0; i < t; i++)
​ {

​ string s;
​ cin>>s;

​ if([Link]("010")!= -1 || [Link]("101")!= -1 )
​ {
​ cout<<"Good"<<endl;
​ }
​ else cout<<"Bad"<<endl;



​ }

​ return 0;
​ }
I. Palindrome

#include<bits/stdc++.h>
using namespace std;

int main()
{
string s,s2;

cin>>s;

s2 = s;

reverse([Link](),[Link]() );

if(s == s2)
{
cout<<"YES"<<endl;

}
else
{
cout<<"NO"<<endl;
}

return 0;
}

Solve with c

​ #include <stdio.h>


​ int main()
​ {


​ char s[1001];
​ scanf("%s",s);
​ int size = strlen(s);




​ int pal=1;
​ for(int i = 0; i<size; i++)
​ {

​ if(s[i]!=s[size-1-i])
​ {
​ pal = 0;
​ break;
​ }


​ }

​ if(pal)
​ {
​ printf("YES\n");
​ }
​ else
​ {
​ printf("NO\n");
​ }



​ return 0;
​ }
J. Count letters

#include<bits/stdc++.h>
using namespace std;

int main()
{
string s;

cin>>s;

int freq[26] = {0};

for(char c : s)
{
freq[c - 'a']++ ;
}

for (int i = 0; i < 26; i++)


{
if(freq[i] > 0)
{
cout<< char(i+'a') <<" : "<< freq[i] <<endl;
}
}

return 0;
}
K. I Love strings

Solve 1

​ #include<bits/stdc++.h>
​ using namespace std;

​ int main()
​ {
​ int n;

​ cin>>n;

​ [Link]();


​ for (int i = 0; i < n; i++)
​ {
​ string s;

​ cin>>s;

​ string t;

​ cin>>t;

​ string s2;

​ int len1 = [Link]();
​ int len2 = [Link]();



​ // cout<<s2<<endl;
​ // cout<<s<<endl;
​ // cout<<t<<endl;


​ int j;

​ for( j = 0; j < len1 && j<len2 ; j++)
​ {

​ cout<<s[j]<<t[j];

​ }

​ while(j<len1)
​ {
​ cout<<s[j];
​ j++;
​ }
​ while(j<len2)
​ {
​ cout<<t[j];
​ j++;
​ }



​ cout<<endl;






​ }


​ return 0;



​ }

Solve 2

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
string s1,s2;

cin>>s1>>s2;

int len1 = [Link]();


int len2 = [Link]();

int len = max(len1,len2);

for (int i = 0; i < len; i++)


{
if(i < len1)
{
cout<<s1[i];
}
if(i < len2)
{
cout<<s2[i];
}
}
cout<<endl;

}
return 0;
}

L. String Functions
Sheet#7(Recursion)
Sheet#3(Array)
K - Sum Digits Codeforces

#include<bits/stdc++.h>
using namespace std;

int main()
{
int n;
cin>>n;
string s;
cin>>s;

long long int sum = 0;

for(char c : s)
{
sum = sum + (c - '0') ;

cout<<sum<<"\n";

return 0;
}
CodeNinjas
Module 19-Problem solving

1.​Is Node Present? - Coding Ninjas

My Solve

bool isNodePresent(BinaryTreeNode<int> *root, int x)


{

if(root == NULL)
{
return false;
}

queue<BinaryTreeNode<int> *> q;
[Link](root);

while(![Link]())
{
//1 ber kore ana - Eject from queue

BinaryTreeNode<int> * f = [Link]();
[Link]();

//2 oi node ke niye kaj

if(f->data == x)
{
return true;
}
//3 push the children

if(f->left != NULL)
{
[Link](f->left);
}

if(f->right != NULL)
{
[Link](f->right);
}

return false;

}
Module Solve

bool isNodePresent(BinaryTreeNode<int> *root, int x)


{

if(root == NULL)
{
return false;
}

if(root->data == x)
{
return true;
}

int l = isNodePresent(root->left,x);
int r = isNodePresent(root->right,x);

return (l || r);

}
2.​Node Level - Coding Ninjas

3.​Left View Of a Binary Tree - Coding Ninjas


4.​Diameter Of Binary Tree - Coding Ninjas

5.​Special Binary Tree. - Coding Ninjas


6.​Reverse Level Order Traversal - Coding Ninjas

House Robber[CodeStudio]

Bottom Up Approach - Loop

int maxMoneyLooted(vector<int> &houses, int n)


{
/*
Write your code here.
Don't write main().
Don't take input, it is passed as function argument.
Don't print output.
Taking input and printing output is handled automatically.
*/

if(n == 0)
{
return 0;
}
vector<int> ans(n);

ans[0] = houses[0];

for(int i = 1 ; i < n ; i++ )


{
if(i == 1)
{
ans[i] = max(houses[0],houses[1]);
}
else
{
int opt1 = houses[i] + ans[i-2];
int opt2 = ans[i-1];

ans[i] = max(opt1,opt2);

return ans[n-1];

}
⚒️ CSES online judge
1.​Message Route - CSES
#include<bits/stdc++.h>
using namespace std;

//A Graph Problem from the online judge CSES


//input
// 5 5
// 1 2
// 1 3
// 1 4
// 2 3
// 5 4

//output

// 3
// 1 4 5

vector<int> adj_list[1000000];
bool vis[1000000];
int lvl[1000000];
int parent[1000000];

void bfs(int src)


{
queue<int> q;
[Link](src);
vis[src] = true;
lvl[src] = 1;
//parent[scr] already -1

while(![Link]())
{
//ber kore ana

int par = [Link]();


[Link]();

//oi node nie kaj

//puch children

for(int child : adj_list[par])


{
if(vis[child] != true)
{
[Link](child);
vis[child] = true;
lvl[child] = lvl[par]+1;
parent[child] = par;
}
}
}

int main()
{
int n,m;
cin>>n>>m;

while(m--)
{
int a,b;
cin>>a>>b;
adj_list[a].push_back(b);
adj_list[b].push_back(a);

memset(vis,false,sizeof(vis));
memset(lvl,-1,sizeof(lvl));

memset(parent,-1,sizeof(parent));
int src = 1;
int des = n;

bfs(src);

if(vis[des] == true)
{
cout<<lvl[des]<<endl;
int node = des;

vector<int> path;

while(node != -1)
{
path.push_back(node);
node = parent[node];
}

reverse([Link](),[Link]());

for(auto x : path)
{
cout<<x<<" ";
}
}
else
{
cout<<"IMPOSSIBLE"<<endl;
}

int node = des;

return 0;
}
2 Shortest Routes II

#include<bits/stdc++.h>
using namespace std;

//undirected Graph

// 4 3 5
// 1 2 5
// 1 3 9
// 2 3 3
// 1 2
// 2 1
// 1 3
// 1 4
// 3 2

int main()
{
long long int n,e,q;

cin>>n>>e>>q;

long long int adj_mat[n+5][n+5];

for (int i = 1; i <= n; i++)


{
for (int j = 1; j <= n; j++)
{
if(i==j)
{
adj_mat[i][j] = 0;

}
else
{
adj_mat[i][j] = LLONG_MAX;

}
}

while(e--)
{
long long int a,b,c;
cin>>a>>b>>c;

adj_mat[a][b] = min(adj_mat[a][b],c);
adj_mat[b][a] = min(adj_mat[b][a],c);

for (int k = 1; k <= n; k++)


{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if(adj_mat[i][k] != LLONG_MAX && adj_mat[k][j] !=
LLONG_MAX &&
adj_mat[i][k] + adj_mat[k][j] < adj_mat[i][j] )
{
adj_mat[i][j] = adj_mat[i][k] + adj_mat[k][j];
}
}

// for (int i = 1; i <= n; i++)


// {
// for (int j = 1; j <= n; j++)
// {
// if(adj_mat[i][j] == INT_MAX)
// {
// cout<<"INF"<<" ";
// }
// else
// {
// cout<<adj_mat[i][j]<<" ";
// }
// }
// cout<<endl;

// }

while(q--)
{

long long int na,nb;

cin>>na>>nb;

if(adj_mat[na][nb] == LLONG_MAX )
{
cout<<-1<<endl;
}
else
{
cout<<adj_mat[na][nb]<<endl;

return 0;
}
[Link] Roads [CSES]

BFS

DFS

#include<bits/stdc++.h>
using namespace std;

//Solved Using DFS

//input
// 4 2
// 1 2
// 3 4

//output
// 1
// 1 3

vector<int> adj[100005];
bool vis[100005];

void dfs(int src)


{
vis[src] = true;

for(int child : adj[src])


{
if(!vis[child])
{
dfs(child);
}
}
}

int main()
{
int n,m;
cin>>n>>m;

vector<int> roads;

while(m--)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);

memset(vis,false,sizeof(vis));

for(int i = 1 ; i<=n ; i++)


{
if(vis[i] == false)
{
dfs(i);
roads.push_back(i);

int cnt = [Link]() - 1;

cout<<cnt<<endl;
for (int i = 0; i < (int)[Link]()-1; i++)
{
cout<<roads[i]<<" "<<roads[i+1]<<endl;
}

return 0;
}
DSU

#include<bits/stdc++.h>
using namespace std;
int par[100005];
int group_size[100005];

int find(int node)


{
if(par[node] == -1)
{
return node;
}
else
{
int parent = find(par[node]);
par[node] = parent;
return parent;
}
}

void dsu_union(int node1,int node2)


{
int leader1 = find(node1);
int leader2 = find(node2);

if(group_size[leader1] >= group_size[leader2] )


{
par[leader2] = leader1;
group_size[leader1] += group_size[leader2];
}
else
{
par[leader1] = leader2;
group_size[leader2] += group_size[leader1];
}

}
int main()
{
int n,e;
cin>>n>>e;

memset(par,-1,sizeof(par));
memset(group_size,1,sizeof(group_size));

// for (int i = 0; i < 100005; i++)


// {
// par[i] = -1;
// group_size[i] = 1;
// }

while(e--)
{
int a,b;
cin>>a>>b;

int leaderA = find(a);


int leaderB = find(b);

if(leaderA != leaderB)
{
dsu_union(a,b);
}
}

int cnt = 0;

vector<int> v;

for (int i = 2; i <= n; i++)


{
int leader_1 = find(1);
int leader_node = find(i);

if(leader_1 != leader_node)
{
cnt++;
dsu_union(leader_1,leader_node);
v.push_back(leader_node);

}
}

cout<<cnt<<endl;

for(int i : v)
{
cout<<1<<" "<<i<<endl;
}

return 0;
}
[Link] Construction [CSES]

DSU

#include<bits/stdc++.h>
using namespace std;
int par[100005];
int group_size[100005];
int cmp ;
int mx ;

int find(int node)


{
if(par[node] == -1)
{
return node;
}
else
{
int parent = find(par[node]);
par[node] = parent;
return parent;
}
}

void dsu_union(int node1,int node2)


{
int leader1 = find(node1);
int leader2 = find(node2);

if(leader1 == leader2)
{
return;
}

if(group_size[leader1] >= group_size[leader2] )


{
par[leader2] = leader1;
group_size[leader1] += group_size[leader2];
mx = max(mx,group_size[leader1]);
}
else
{
par[leader1] = leader2;
group_size[leader2] += group_size[leader1];
mx = max(mx,group_size[leader2]);
}

cmp--;

int main()
{
int n,e;
cin>>n>>e;

cmp = n;

mx = 1;

// memset(par,-1,sizeof(par));
// memset(group_size,1,sizeof(group_size));

for (int i = 0; i < 100005; i++)


{
par[i] = -1;
group_size[i] = 1;
}
while(e--)
{
int a,b;
cin>>a>>b;

dsu_union(a,b);
cout<<cmp<<" "<<mx<<endl;

return 0;
}
5 Concert Tickets

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
[Link](NULL);
int n,m;
cin>>n>>m;
multiset<int> tickets;
// vector<int> tickets(n);
for (int i = 0; i < n; i++)
{
int val;
cin>>val;
[Link](val);
}
// for (int i = 0; i < n; i++)
// {
// cin>>tickets[i];
// }

// vector<int> mx_price(m);
for (int i = 0; i < m; i++)
{
int mx_price;
cin>>mx_price;
auto it = tickets.upper_bound(mx_price); //upper_bound
returns next greater number
if(it == [Link]()) //means all tickets are greater
then the max price
{
cout<<-1<<'\n';
}
else
{
it--;
cout<<*it<<'\n';
[Link](it);
}
}
return 0;
}
Problem Solvers Club

Sum of Two Values

CSES - Playlist

Distinct Values Subarrays

Distinct Values Subarrays II


Mentor-Assignments
Submission Deadline : 20th August, 2025​

No. Problem Link

1 Problem 1

2 Problem 2

3 Problem 3

4 Problem 4

5 Problem 5

6 Problem 6

7 Problem 7

8 Problem 8

9 Problem 9

10 Problem 10

11 Problem 11

12 Problem 12

13 Problem 13

14 Problem 14

15 Problem 15

16 Problem 16

17 Problem 17

18 Problem 18

19 Problem 19

20 Problem 20
Problem 1

Solve

#include<bits/stdc++.h>
using namespace std;

int main()
{
char s[101];

cin>>s;

int size = strlen(s);

int upcount = 0;
int lowcount = 0;

for (int i = 0; i < size; i++)


{
if( 'a' <= s[i] && s[i] <= 'z')
{
lowcount++;
}
else if( 'A' <= s[i] && s[i] <= 'Z')
{
upcount++;
}

// cout<<upcount<<endl;
// cout<<lowcount<<endl;
if(upcount>lowcount)
{
for (int i = 0; i < size; i++)
{
if( 'a' <= s[i] && s[i] <= 'z')
{
s[i] = s[i] - 32;
}

}
else if(upcount<lowcount)
{
for (int i = 0; i < size; i++)
{
if( 'A' <= s[i] && s[i] <= 'Z')
{
s[i] = s[i] + 32;
}

}
else
{
for (int i = 0; i < size; i++)
{
if( 'A' <= s[i] && s[i] <= 'Z')
{
s[i] = s[i] + 32;
}

}
}

cout<<s;
return 0;
}
Problem 19

Solve

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a1(n),a2(n);
for (int i = 0; i < n; i++)
{
cin>>a1[i];
}
for (int i = 0; i < n; i++)
{
a2[i] = a1[i] ;
}
sort([Link](),[Link]());
int l = 0;
int r = n-1;

while (l<n && a1[l] == a2[l] )


{
l++;
}

while (r>=0 && a1[r] == a2[r] )


{
r--;
}

if(l>=n)
{
cout<<"yes"<<endl;
cout<<1<<" "<<1;
return 0 ;

reverse([Link]()+l,[Link]()+r+1);

if(a1==a2)
{
cout<<"yes"<<endl;
cout<<l+1<<" "<<r+1;
}
else
{
cout<<"no"<<endl;
}

return 0;
}
XPSC
DSA Overview Session Week-1,2,3 14/09/2025

[Link]

[Link]

[Link]

Practice List
[Link]

DSA Overview Session Week-4,5,6 16/09/2025

Top K Frequent Elements[leetCode]Medium

class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
map<int,int> mp;

for(auto x : nums)
{
mp[x]++;
}

priority_queue<pair<int,int>> pq;

for(auto [elem,freq] : mp)


{
[Link]({freq,elem});
}

vector <int> res;


for(int i = 0 ; i < k ; i++)
{
auto p = [Link]();
res.push_back([Link]);
[Link]();
}

return res;
}
};

ITA-Week-04-Conceptual Session 2

W Reach Value [Codeforces-Recursion]

Bottom Up -Recursion

#include<bits/stdc++.h>
using namespace std;

//codeforces

//Bottom-Up-Recursion

bool rec(long long int cnt, long long int n)


{
if(cnt > n)
{
return false;
}

if(cnt == n)
{
return true;
}

bool op1 = rec(cnt * 10,n);


bool op2 = rec(cnt * 20,n);

return op1 || op2;

int main()
{
int t;
cin>>t;

while(t--)
{
long long int n;
cin>>n;

if(rec(1,n))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
Top-Down Recursion

#include<bits/stdc++.h>
using namespace std;

//codeforces

//Top-Down-Recursion

bool rec(long long int n)


{
if(n == 1)
{
return true;
}

if(n == 0)
{
return false;
}
bool op1 = false,op2 = false;
if(n%10 == 0 )
{
op1 = rec(n/10);

}
if(n%20 == 0)
{
op2 = rec(n/20);
}

return op1 || op2;

int main()
{
int t;
cin>>t;

while(t--)
{
long long int n;
cin>>n;

if(rec(n))
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
Self Practice

E. Base Conversion

#include <bits/stdc++.h>
using namespace std;

void binary(long long int n)


{

if (n == 0)
{
return ;
}

binary(n/2);
cout<<(n%2);
}

int main()
{
long long int t;
cin>>t;

while(t--)
{
long long int n;
cin>>n;
if(n==0)
{
cout<<0;
}
else
{
binary(n);

cout<<endl;
}

return 0;
}

F Print Even Indices[CodeForces-Recursion]

Top-Down-Recursion

#include<bits/stdc++.h>
using namespace std;
long long int a[1005];

void rec(int i)
{
if(i<0)
{
return ;
}

if(i % 2 == 0)
{
cout<<a[i]<<" ";
}
rec(i-1);

int main()
{
long long int n;
cin>>n;

for (long long int i = 0; i < n; i++)


{

cin>> a[i];
}

rec(n-1);

cout<<endl;
return 0;
}
Phitron Exams
Profile Link :
[Link]
User Name : @ahmedragibhasan7

ios_base::sync_with_stdio(false);
[Link](NULL);

[Link]
es

Algorithm Mid

[Link]
allenges

Algorithm Assignment 2

[Link]
#include <bits/stdc++.h>
#define ll long long int
#define all(x) [Link](), [Link]()
#define nl '\n'
#define fastIO() ios_base::sync_with_stdio(0), [Link](0), [Link](0)
using namespace std;
#ifndef ONLINE_JUDGE
// #include "../DebugTemplate/debug.h"
#else
#define debug(x...)
#define dbgsize(x)
#endif
int main()
{
fastIO();
return 0;
}
Temp
Sphere Online Judge
[Link]
GeeksForGeeks Online Judge
1.​Bellman-Ford [GFG]

// User function Template for C++

class Solution {
public:

class Edge
{
public:
int a,b,c;
Edge(int a,int b,int c)
{
this->a = a;
this->b = b;
this->c = c;
}
};

vector<int> bellmanFord(int V, vector<vector<int>>& edges, int src) {


// Code here
vector<Edge> edge_list;

for(auto ed : edges)
{
edge_list.push_back(Edge(ed[0],ed[1],ed[2]));
}

vector<int> dis(V);
for (int i = 0; i < V; i++)
{
dis[i] = 1e8;
}

dis[src] = 0;

for (int i = 0; i < V - 1; i++) // o(v)


{
for (auto ed : edge_list) // O(E)
{
int a, b, c;

a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != 1e8 && dis[a] + c < dis[b])
{
dis[b] = dis[a] + c;
}
}
}

for (auto ed : edge_list) // O(E)


{
int a, b, c;

a = ed.a;
b = ed.b;
c = ed.c;
if (dis[a] != 1e8 && dis[a] + c < dis[b])
{
return { -1};
}
}

return dis;

}
};

Disjoint set (Union-Find) [GFG]

/*Complete the functions below*/


int find(int par[], int x) {
// add code here
if(par[x] == x )
{
return x;
}
else
{
int parent = find(par,par[x]);
par[x] = parent;
return parent;
}
}

void unionSet(int par[], int x, int z) {


int leader1 = find(par,x);
int leader2 = find(par,z);

par[leader1] = leader2;

}
CodeChef
Covered In Phitron Sessions

Mixing Liquids - Div-4

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
int a,b;
cin>>a>>b;

int cnt = 0;

while(true)
{
a--;
b -= 2;
if(a<0 || b<0)
{
break;
}
else
{
cnt +=3;
}

cout<<cnt<<endl;
}

return 0;
}
Mark Points Div-4

#include<bits/stdc++.h>
using namespace std;

void solve()
{
int n;

cin>>n;

string s;

cin>>s;

int cnt = 0;

bool flag = true;

for (int i = 0; i < n; i++)


{
if(s[i] == '1' )
{
cnt++;

}
else
{
if(cnt == 1 || cnt == 2)
{
flag = false;
break;
}

cnt = 0;
}

}
if(cnt == 1 || cnt == 2)
{
flag = false;

if(flag)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}

int main()
{
int t;
cin>>t;

while(t--)
{
solve();
}
return 0;
}

Two Roll - Div-4

#include<bits/stdc++.h>
using namespace std;
void solve()
{
int x,y;

cin>>x>>y;

bool flag = false;

for (int i = 0; i <= 5; i++)


{
int d1 = y + i;
for (int j = 0; j <= 5; j++)
{
int d2 = y + j;
if(x + d1 + d2 == 50)
{
flag = true;
}
}

if(flag)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}

int main()
{
int t;
cin>>t;

while(t--)
{
solve();
}
return 0;
}
Self Practice

[Link]

#include <bits/stdc++.h>
using namespace std;

int main() {

int a,b;
cin>>a>>b;

cout<<180-(a+b)<<endl;

[Link]

#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;

while(t--)
{
int n,k;
cin>>n>>k;

int sum_time = n * k;

int hours = sum_time / 60;

int minutes = sum_time % 60;

cout<<hours<<" "<<minutes<<endl;

return 0;
}
Outslook Online Judge
LeetCode-MasterBranch
Palindrome Number[easy-Math]

Retarded Solution

class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
{
return false;
}

vector<int> v;
vector<int> v2;

while(x != 0)
{
int last = x % 10;
v.push_back(last);
x = x / 10;
}

v2 = v;

reverse([Link](),[Link]());

if(v == v2)
{
return true;
}
else
{
return false;
}
}
};

House-robber[Medium]
Solve
LeetCode Grid/Matrix Problems
1.​Island Perimeter [Easy] ​
- Asked in [Google, Facebook, Amazon, Apple, Microsoft] ​
solution link​

2.​Find if path exists in graph [Easy]​


- Asked in [Microsoft] ​
solution link​

3.​Max area of island [Medium]​


- Asked in [Google, Facebook, Amazon, Microsoft] ​
solution link​

4.​Number of islands [Medium]​


- Asked in [Google, Facebook, Amazon, Apple, Microsoft,
LinkedIn, Snapchat, Twitter, Uber] ​
solution link​

5.​Count sub islands [Medium]​


- Asked in [Twitter] ​
solution link​

6.​Number of closed islands [Medium]​


- Asked in [Google] ​
solution link
7 Nearest Exit from Entrance in Maze [Medium]​
- Asked in [Google, Amazon] ​
solution link

class Solution {
public:

bool vis[105][105];
int level[105][105];
vector<pair<int,int>> d = {{1,0},{-1,0},{0,-1},{0,1}};
int n,m;

bool valid(int i,int j)


{
if(i<0 || j<0 || i>=n || j>=m )
{
return false;
}
return true;
}

int bfs(int si,int sj, vector<vector<char>>& maze)


{
queue<pair<int,int>> q;
[Link]({si,sj});
vis[si][sj] = true;
level[si][sj] = 0;
while(![Link]())
{
pair<int,int> par = [Link]();
[Link]();
int par_i = [Link];
int par_j = [Link];
for(int i = 0 ; i<4 ; i++)
{
int ci = par_i + d[i].first;
int cj = par_j + d[i].second;
if(!valid(ci,cj) && (par_i != si || par_j !=
sj))
{
return level[par_i][par_j];
}
if(valid(ci,cj) && !vis[ci][cj] &&
maze[ci][cj]=='.' )
{
[Link]({ci,cj});
vis[ci][cj] = true;
level[ci][cj] = level[par_i][par_j]+1;
}

}
return -1;

int nearestExit(vector<vector<char>>& maze, vector<int>&


entrance) {

n = [Link]();
m = maze[0].size();

memset(vis,false,sizeof(vis));
return bfs(entrance[0],entrance[1],maze);
}

};
70 Leetcode Problems
1 Contains Duplicate - LeetCode

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {

set<int> s;

for(int x: nums)
{
[Link](x);
}

return [Link]() != [Link]();

}
};

2 Missing Number - LeetCode

class Solution {
public:

int missingNumber(vector<int>& nums) {

int n = [Link]() ;
sort([Link](),[Link]());

int miss;

for(int i = 0 ; i < n ; i++)


{
if(nums[i] != i )
{
miss = i;
break;
}

return miss;

}
};
3 Two Sum - LeetCode

Brute Force - Bad Complexity - but accepted

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = [Link]();

vector<int> ans;

for(int i = 0 ; i<n ; i++)


{
for(int j =i + 1 ; j<n ; j++)
{
if(nums[j] == target - nums[i])
{
ans.push_back(i);
ans.push_back(j);
break;

}
}
}

return ans;

}
};
4 How Many Numbers Are Smaller Than the Current Number - LeetCode

class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {

vector<int> smaller;

int n = [Link]();

int cnt = 0;

for(int i = 0 ; i< n ; i++)


{
for(int j=0; j<n ; j++)
{
if(nums[i] != nums[j] && nums[i] > nums[j] )
{
cnt++;
}

}
smaller.push_back(cnt);
cnt = 0;
}

return smaller;

}
};

5 Minimum Time Visiting All Points - LeetCode

( didn’t get solution, Study again )


(Chebyshev Distance)

class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
//Chebyshev Distance

int sec = 0;

int n = [Link]();
for(int i = 0 ; i < n-1 ; i ++ )
{
int srcx = points[i][0];
int srcy = points[i][1];

int desx = points[i+1][0];


int desy = points[i+1][1];

sec += max(abs(desx - srcx) , abs(desy - srcy));

return sec;
}
};
Shariah
[Link]
'লা ইলাহা ইল্লা আনতা সুবহানাকা ইন্নী কুনতু মিনায যা-লিমীন।'
অর্থাৎ, তু মি ব্যতীত কোনো উপাস্য নেই; তু মি পবিত্র, মহান! নিশ্চয় আমি অত্যাচারীদের অন্তর্ভু ক্ত।

Time-table

Today Islamic Prayer Timings in Dhaka, Bangladesh

After Salah Sunnah

[Link]

Full Course | Understand Quran and Salaah Easy Way


| illustrated | 100 Episodes | Learn Quran Arabic

Full Course | Understand Quran and Salaah Easy Way | illustrated | 100 Episodes | Learn …

উচ্চারণ: লা ইলাহা ইল্লাল্লাহু ওয়াহদাহু লা শারীকা লাহু, লাহুল মুলকু


ু ওয়া লাহুল হামদু ওয়া হুয়া আলা কুল্লি শাইয়্যিন ক্বাদীর।
আলহামদুলিল্লাহি ওয়া সুবহানাল্লাহি ওয়ালা ইলাহা ইল্লাল্লাহু ওয়াল্লাহু আকবার, ওয়ালা হাওলা ওয়ালা ক্বুওয়াতা ইল্লা বিল্লাহ।
অর্থ: ‘‘এক আল্লাহ্ ব্যতীত প্রকৃ ত কোন ইলাহ নেই। তিনি এক তাঁর কোন শরীক নেই। রাজ্য তাঁরই। যাবতীয় প্রশংসা তাঁরই।
তিনিই সব কিছু র উপরে শক্তিমান। যাবতীয় প্রশংসা আল্লাহরই জন্য, আল্লাহ্ তা‘আলা পবিত্র, আল্লাহ্ ব্যতীত সত্য কোন ইলাহ
নেই। আল্লাহ্ মহান, গুনাহ হতে বাঁচার এবং নেক কাজ করার কোন শক্তি নেই আল্লাহর তাওফীক ব্যতীত।’’
অতঃপর বলে,
ْ ‫اللَّ ُه َّم‬
‫اغفِرْ لِي‬
উচ্চারণ: আল্লাহুম্মাগফিরলী
অর্থ: ‘‘হে আল্লাহ্! আমাকে ক্ষমা করুন।’’
[Link]
Tab 68
আল্লাহুম্মা ইন্নাকা আফু উন তু হিব্বুল্ আফওয়া ফা’ফু আন্নী

‘লাইলাতু ল্ কদর’ এ কি কি ইবাদত করবেন?


অনেক দ্বীনী ভাই আছেন যারা সহীহ নিয়মে লাইলাতু ল কদরে ইবাদত করতে ইচ্ছুক। তাই তারা প্রশ্ন করে থাকেন যে, লাইলাতু ল
কদরে আমরা কি কি ইবাদত করতে পারি? এই রকম ভাই এবং সকল মুসলিম ভাইদের জ্ঞাতার্থে সংক্ষিপ্তাকারে কিছু উল্লেখ করা
হল। [ওয়ামা তাওফীকী ইল্লা বিল্লাহ]
প্রথমতঃ আল্লাহ তাআ’লা আমাদের বলে দিয়েছেন যে, এই রাত এক হাজার মাসের থেকেও উত্তম। অর্থাৎ এই এক রাতের
ইবাদত এক হাজার মাসের থেকেও উত্তম। [আল্ মিসবাহ আল্ মুনীর/১৫২১]
তাই এই রাতটি ইবাদতের মাধ্যমে অতিবাহিত করাই হবে আমাদের মূল উদ্দেশ্য।
দ্বিতীয়তঃ জানা দরকার যে ইবাদত কাকে বলে? ইবাদত হচ্ছে, প্রত্যেক এমন আন্তরিক ও বাহ্যিক কথা ও কাজ যা, আল্লাহ পছন্দ
করেন এবং তাতে সন্তুষ্ট থাকেন। [মাজমুউ ফাতাওয়া,১০/১৪৯]
উক্ত সংজ্ঞার আলোকে বলা যেতে পারে যে ইবাদত বিশেষ এক-দুটি কাজে সীমাবদ্ধ নয়। তাই আমরা একাধিক ইবাদতের
মাধ্যমে এই রাতটি অতিবাহিত করতে পারি। নিম্নে কিছু উৎকৃ ষ্ট ইবাদত উল্লেখ করা হলঃ
১- ফরয নামায সমূহ ঠিক সময়ে জামাআ’তের সাথে আদায় করা।
যেমন মাগরিব, ইশা এবং ফজরের নামায। তার সাথে সাথে সুন্নতে মুআক্কাদা, তাহিয়্যাতু ল মসজিদ সহ অন্যান্য মাসনূন নামায
আদায় করা।
২- কিয়ামে লাইলাতু ল্ কদর করা।
অর্থাৎ রাতে তারবীহর নামায আদায় করা। নবী (সাঃ) বলেনঃ
“যে ব্যক্তি ঈমান ও নেকীর আশায় লাইলাতু ল কদরে কিয়াম করবে (নামায পড়বে) তার বিগত গুনাহ ক্ষমা করা হবে”।
[ফাতহুল বারী,৪/২৯৪]
এই নামায জামাআতের সাথে আদায় করা উত্তম। অন্যান্য রাতের তু লনায় এই রাতে ইমাম দীর্ঘ কিরাআতের মাধ্যমে নামায
সম্পাদন করতে পারেন। ইশার পর প্রথম রাতে কিছু নামায পড়ে বাকী নামায শেষ রাতে পড়াতে পারেন। একা একা নামায
আদায়কারী হলে সে তার ইচ্ছানুযায়ী দীর্ঘক্ষণ ধরে নামায পড়তে পারে।
৩- বেশী বেশী দুআ করা।
তন্মধ্যে সেই দুআটি বেশী বেশী পাঠ করা যা নবী (সাঃ) মা আয়েশা (রাযিঃ) কে শিখিয়েছিলেন।
মা আয়েশা নবী (সাঃ) কে জিজ্ঞাসা করেনঃ হে আল্লাহর রাসূল! যদি আমি লাইলাতু ল কদর লাভ করি, তাহলে কি দুআ করবো?
তিনি (সাঃ) বলেনঃ বলবে, (আল্লাহুম্মা ইন্নাকা আফু উন তু হিব্বুল্ আফওয়া ফা’ফু আন্নী”। [আহমদ,৬/১৮২] অর্থ, হে আল্লাহ!
তু মি ক্ষমাশীল। ক্ষমা পছন্দ কর, তাই আমাকে ক্ষমা কর”।
এছাড়া বান্দা পছন্দ মত দুনিয়া ও আখেরাতের কল্যাণকর যাবতীয় দুআ করবে। সে গুলো প্রমাণিত আরবী ভাষায় দুআ হোক
কিংবা নিজ ভাষায় হোক। এ ক্ষেত্রে ইবাদতকারী একটি সুন্দর সহীহ দুআ সংকলিত দুআর বইয়ের সাহায্য নিতে পারে। সালাফে
সালেহীনদের অনেকে এই রাতে অন্যান্য ইবাদতের চেয়ে দুআ করাকে অগ্রাধিকার দিয়েছেন। কারণ এতে বান্দার মুক্ষাপেক্ষীতা,
প্রয়োজনীয়তা ও বিনম্রতা প্রকাশ পায়, যা আল্লাহ পছন্দ করেন।
৪- যিকর আযকার ও তাসবীহ তাহলীল করা।
অবশ্য এগুলো দুআরই অংশ বিশেষ। কিন্তু বিশেষ করে সেই শব্দ ও বাক্য সমূহকে যিকর বলে, যার মাধ্যমে আল্লাহর প্রশংসা ও
গুণগান করা হয়। যেমন, “লা ইলাহা ইল্লাল্লাহ”, “আল্ হামদু ল্লিল্লাহ” “সুবহানাল্লাহ”, “আল্লাহুআকবার” “আস্তাগফিরুল্লাহ”, “লা
হাওলা ওয়ালা কুউআতা ইল্লা বিল্লাহ”। ইত্যাদি।
৫- কুরআন তিলাওয়াত।
কুরআন পাঠ একটি বাচনিক ইবাদত, যা দীর্ঘ সময় ধরে করা যেতে পারে। যার এক একটি অক্ষর পাঠে রয়েছে এক একটি
নেকী। নবী (সাঃ) বলেনঃ
“যে ব্যক্তি আল্লাহর কিতাবের একটি অক্ষর পড়বে, সে তার বিনিময়ে একটি নেকী পাবে… আমি একথা বলছি না যে,
আলিফ,লাম ও মীম একটি অক্ষর; বরং আলিফ একটি অক্ষর লাম একটি অক্ষর এবং মীম একটি অক্ষর”। [তিরমিযী, তিনি
বর্ণনাটিকে হাসান সহীহ বলেন]
এছাড়া কুরআন যদি কিয়ামত দিবসে আপনার সুপারিশকারী হয়, তাহলে কতই না সৌভাগ্যের বিষয়! নবী (সাঃ) বলেনঃ
“তোমরা কুরআন পড়; কারণ সে কিয়ামত দিবসে পাঠকারীর জন্য সুপারিশকারী হিসাবে আগমন করবে”। [মুসলিম]
৬- সাধ্যমত আল্লাহর রাস্তায় কিছু দান-সাদকা করা।
নবী (সাঃ) বলেনঃ
“সাদাকা পাপকে মুছে দেয়, যেমন পানি আগুনকে নিভিয়ে দেয়”। [সহীহুত তারগবি]
শবে কদরের একটি রাতে এই রকম ইবাদতের মাধ্যমে আপনি ৮৩ বছর ৪ মাসের সমান সওয়াব অর্জ ন করতে পারেন।
ইবাদতের এই সুবর্ণ সুযোগ যেন হাত ছাড়া না হয়। আল্লাহ আমাদের তাওফীক দিন। আমীন!
উল্লেখ থাকে যে, ইবাদতের উদ্দেশ্যে বৈষয়িক কাজ-কর্মও ইবাদতে পরিণত হয়। যেমন রোযার উদ্দেশ্যে সাহরী খাওয়া, রাত
জাগার জন্য প্রয়োজনীয় কাজ-কর্ম সেরে নেওয়া। তাই লাইলাতু ল কদরে ইবাদতের উদ্দেশ্যে বান্দা যেসব দুনিয়াবী কাজ করে
সেগুলোও ইবাদতের অন্তর্ভু ক্ত।
আশা করি আপনাদের বিষয়ের সাথে সম্পৃক্ত কিছু আইডিয়া দিতে পেরেছি। ওয়ামা তাওফীক ইল্লা বিল্লাহ্
Tab 65
গোসলের ফরজ ৩ টিঃ

১. গড়গড়া সহ কুলি করা, যাতে পানি গলার হাড় পর্যন্ত পৌছে।

২. হাতে পানি নিয়ে নাকের নরম হাড় পর্যন্ত পানি পৌছানো।

৩. সমস্ত শরীর উত্তম রুপে ধৌত করা।

ফরজ গোসলের সঠিক নিয়মঃ

১গোসলের নিয়ত করা, 'বিসমিল্লাহ' বলে গোসল শুরু করা। দুই হাত কবজি পর্যন্ত ধোওয়া (বুখারী ২৪৮)

২ পানি ঢেলে বাম হাত দিয়ে লজ্জাস্থান পরিষ্কার করা (বুখারী ২৫৭)

৩ বাম হাতটি ভালভাবে ঘষে ধুয়ে নেওয়া (বুখারী ২৬৬)

৪ নামাজের ওজুর মতো ভালভাবে পূর্ণরূপে ওজু করা। এক্ষেত্রে শুধু পা দুটো বাকি রাখলেও চলবে, যা গোসলের শেষে ধুয়ে
ফেলতে হবে। (বুখারী ২৫৭, ২৫৯, ২৬৫)।

৫ মাথায় পানি ঢেলে চু লের গোড়া ভালভাবে আঙ্গুল দিয়ে ভিজানো। (বুখারী ২৫৮)।

৬ পুরো শরীরে পানি ঢালা; প্রথমে ডানে ৩বার, পরে বামে ৩বার, শেষে মাথার উপর ৩ বার (বুখারী ১৬৮)।

৭ (যেন শরীরের কোন অংশ বা কোন লোমও শুকনো না থাকে। পুরুষের দাড়ি ও মাথার চু ল এবং মহিলাদের চু ল ভালোভাবে
ভিজতে হবে।

নাভি, বগল ও অন্যান্য কুঁ চকানো জায়গায় অবশ্যই পানি ঢালতে হবে)।

গোসলের জায়গা থেকে একটু সরে গিয়ে দুই পা ধোওয়া। (বুখারী ২৫৭)।
যেকোনো হালাল বিষয়ে সিদ্ধান্ত নেওয়ার আগে ইস্তিখারা করুন!

ইস্তিখারা কী?

ইস্তিখারা অর্থ হলো—

আল্লাহর কাছে কল্যাণ চাওয়া।

*যখন আমরা কোনো গুরুত্বপূর্ণ সিদ্ধান্তে দ্বিধায় পড়ি (বিয়ে, চাকরি, ব্যবসা, পড়াশোনা, কোথাও যাওয়া ইত্যাদি), তখন আল্লাহর কাছে সাহায্য
চাইতেই ইস্তিখারা করা হয়।

ইস্তিখারা কেন পড়তে হয়?

কারণ—

- আমরা ভবিষ্যৎ জানি না

- কোনটা আমাদের জন্য ভালো, কোনটা খারাপ—তা শুধু আল্লাহ জানেন

তাই ইস্তিখারার মাধ্যমে আমরা বলি:

“হে আল্লাহ, আপনি জানেন, আমি জানি না—আমার জন্য যেটা ভালো সেটাই করে দিন।”

*ইস্তিখারা কখন পড়বেন?*

- যেকোনো হালাল বিষয়ে সিদ্ধান্ত নেওয়ার আগে

- ফরজ বা হারাম বিষয়ে ইস্তিখারা নেই

ইস্তিখারা কীভাবে পড়তে হয়?


১. ভালোভাবে অজু করবেন

২. দুই রাকাত নফল নামাজ পড়বেন

(ফরজ নামাজের সাথে মিলাবেন না)

৩. নামাজ শেষে ইস্তিখারার দোয়া পড়বেন

৪. দোয়ার সময় নিজের প্রয়োজনের কথা মনে মনে বলবেন

ইস্তিখারার দু'আ:

কোন কাজে ভালো মন্দ বুঝতে না পারলে, মনে ঠিক-বেঠিক, উচিত-অনুচিত বা লাভ-নোকসানের দ্বন্দ্ব আল্লাহর নিকট মঙ্গল প্রার্থনা করতে দুই
রাকআত নফল নামায পড়ে নিম্নের দুআ পঠনীয়।

َ‫ اللَّ ُه َّم ِإنْ ُك ْنتَ َتعْ َل ُم َأنَّ ه ََذا اَألمْ ر‬، ‫ب‬
ِ ‫ َو َتعْ َل ُم َوال َأعْ َل ُم َوَأ ْنتَ عَ ال ُم ْال ُغيُو‬، ‫ َفِإ َّنكَ َت ْق ِد ُر َوال َأ ْق ِد ُر‬، ‫ َوَأسْ َألُكَ مِنْ َفضْ لِكَ ْالعَ ظِ ِيم‬، َ‫ َوَأسْ َتعِي ُنكَ ِبقُ ْدرَ تِك‬، َ‫اللَّ ُه َّم ِإ ِّني َأسْ َتخِيرُكَ ِبع ِْلمِك‬
‫َاركْ لِي فِي ِه َوِإنْ ُك ْنتَ َتعْ َل ُم َأنَّ ه ََذا اَألمْ رَ َشرٌّ لِي فِي دِينِي َومَعَ اشِ ي َوعَ ا ِق َب ِة مْ ِري‬
‫َأ‬ ُ ‫َأ‬
ِ ‫ َفا ْقدُرْ هُ لِي َو َيسِّرْ هُ لِي ث َّم ب‬، ‫() َخ ْي ٌر لِي فِي دِينِي َومَعَ اشِ ي َوعَ ا ِق َب ِة مْ ِري َوعَ ا ِجلِ ِه َوآ ِجلِ ِه‬
‫ضنِي ِب ِه‬ ِّ َ‫ ُث َّم ر‬، َ‫ْث َكان‬ ُ ‫ َوا ْقدُرْ لِيَ ْال َخ ْيرَ حَ ي‬، ‫ َواصْ ِر ْفنِي عَ ْن ُه‬، ‫َوعَ ا ِجلِ ِه َوآ ِجلِ ِه َفاصْ ِر ْف ُه عَ ِّني‬

উচ্চারণঃ আল্লা-হুম্মা ইন্নী আস্‌


তাখীরুকা বিইলমিকা অ আস্‌
তাক্‌
দিরুকা বি কুদরাতিকা অ আসআলুকা মিন ফায্বলিকাল আযীম, ফাইন্নাকা তাক্‌
দিরু
অলা আক্‌
দিরু অতা’লামু অলা আ’লামু অ আন্তা আল্লা-মুল গুয়ূব। আল্লা-হুম্মা ইন কুন্তা তালামু আন্না হা-যাল আমরা ( ) খাইরুল লী ফী দীনী অ
মাআ’শী অ আ’কিবাতি আমরী অ আ’-জিলিহী অ আ-জিলিহ, ফাক্‌
দর
ু হু লী, অ য়্যাসসিরহু লী, সুম্মা বা-রিক লী ফীহ। অ ইন কুন্তা তা'লামু আন্না
হা-যাল আমরা শাররুল লী ফী দীনী অ মাআ’শী অ আ’-কিবাতি আমরী অ আ’-জিলিহী অ আ-জিলিহ, ফাস্বরিফহু আন্নী অস্বরিফনী আনহু, অক্বদুর
লিয়াল খাইরা হাইসু কা-না সুম্মা রায্বযিনী বিহ।

অর্থঃ হে আল্লাহ! নিশ্চয় আমি তোমার নিকট তোমার ইলমের সাথে মঙ্গল প্রার্থনা করছি। তোমার কুদরতের সাথে শক্তি প্রার্থনা করছি এবং তোমার
বিরাট অনুগ্রহ থেকে ভিক্ষা যাচনা করছি। কেননা, তু মি শক্তি রাখ, আমি শক্তি রাখি না। তু মি জান, আমি জানি না এবং তু মি অদৃশ্যের পরিজ্ঞাতা।
হে আল্লাহ! যদি তু মি এই ( ) কাজ আমার জন্য আমার দ্বীন, দুনিয়া, জীবন এবং কাজের বিলম্বিত ও অবিলম্বিত পরিণামে ভালো জান, তাহলে তা
আমার জন্য নির্ধারিত ও সহজ করে দাও। অতঃপর তাতে আমার জন্য বৰ্ক ত দান কর। আর যদি তু মি এই কাজ আমার জন্য আমার দ্বীন, দুনিয়া,
জীবন এবং কাজের বিলম্বিত ও অবিলম্বিত পরিণামে মন্দ জান, তাহলে তা আমার নিকট থেকে ফিরিয়ে নাও এবং আমাকে ওর নিকট থেকে সরিয়ে
দাও। আর যেখানেই হোক মঙ্গল আমার জন্য বাস্তবায়িত কর, অতঃপর তাতে আমার মনকে পরিতু ষ্ট করে দাও।

প্রথমে ( َ‫‘ )ه ََذا اَألمْ ر‬হা-যাল আমরা এর স্থলে বা পরে কাজের নাম নিতে হবে অথবা মনে মনে সেই জ্ঞাতব্য বিষয়ের প্রতি ইঙ্গিত করতে হবে।

সে ব্যক্তি কর্মে কোনদিন লাঞ্ছিত হয় না, যে আল্লাহর নিকট তাতে মঙ্গল প্রার্থনা করে, অভিজ্ঞদের নিকট পরামর্শ গ্রহণ করে এবং ভালো-মন্দ বিচার
করার পর কর্ম করে। (বুখারী ৭/ ১৬২, আবু দাউদ ২/৮৯, তিরমিযী ২/৩৫৫, আহমাদ ৩/৩৪৪)।

ইস্তিখারার পর কী হবে?

• স্বপ্ন দেখা জরুরি নয়।

• মন যেদিকে স্বস্তি পায়

• কাজটি সহজ হয়ে যায় বা দূরে সরে যায়

—এভাবেই আল্লাহ সিদ্ধান্ত স্পষ্ট করেন

বিশ্বাস রাখুন

ইস্তিখারার পর আল্লাহ যা ঘটাবেন—সেটাই আপনার জন্য সবচেয়ে ভালো।

আল্লাহ আমাদের সঠিক সিদ্ধান্ত নেওয়ার তাওফিক দিন।আমিন।


Tab 75
[Link]

জুমার দিন কি শুধু গোসল, নামাজ আর খুতবাতেই শেষ হয়ে যায়?

অনেকেই জুমাকে সম্মান করেন,


কিন্তু জুমার ভেতরে লুকিয়ে থাকা বরকতের দরজাগুলো পুরোভাবে কাজে লাগান না।

অথচ এই দিনটা শুধু সাপ্তাহিক ইবাদতের দিন না।


এটা রহমতের দিন।
এটা দোয়ার দিন।
এটা মাগফিরাতের দিন।
এটা এমন এক দিন, যেখান থেকে পুরো সপ্তাহের জন্যও বরকত তোলা যায়।

বিশেষ করে আপনি যদি রিজিক, ঋণ, ব্যবসা, চাকরি, সংসারের স্বস্তি—এসব নিয়ে চিন্তায় থাকেন, তাহলে জুমার দিনটা
আপনার জন্য খুবই মূল্যবান।

আজ জানুন জুমার ৩টা আমল,


যা আপনার সপ্তাহকে আরও বরকতময় করতে পারে, ইনশাআল্লাহ।

১. জুমার দিনে বেশি বেশি দরূদ পড়ুন

নবী করিম ‫ ﷺ‬বলেছেন, তোমাদের শ্রেষ্ঠ দিনগুলোর একটি হলো জুমার দিন। তাই এ দিনে আমার উপর বেশি বেশি দরূদ
পাঠ করো।

জুমার দিনে দরূদ পড়ার ভেতরে অদ্ভু ত এক নূর আছে।


এটা অন্তরকে নরম করে।
দোয়ার আদব তৈরি করে।
আর আল্লাহর রহমত টেনে আনে।

আপনি এভাবে পড়তে পারেন—

আরবি:
َ ‫اللَّ ُه َّم‬
ِ ‫ص ِّل َع َلى م َُح َّم ٍد َو َع َلى‬
‫آل م َُح َّم ٍد‬

বাংলা উচ্চারণ:
আল্লাহুম্মা সাল্লি আলা মুহাম্মাদিওঁ ওয়া আলা আলি মুহাম্মাদ।

বাংলা অর্থ:
হে আল্লাহ, নবী করিম ‫ ﷺ‬ও তাঁর পরিবারবর্গের উপর রহমত নাযিল করুন।

চাইলে দরূদে ইব্রাহীমীও পড়তে পারেন।


জুমার দিন অন্তত ১০০ বার দরূদ পড়ার নিয়ত করতে পারেন।

২. সূরা কাহফ পড়ুন


নবী করিম ‫ ﷺ‬থেকে বর্ণিত আছে,
যে ব্যক্তি জুমার দিনে সূরা কাহফ পড়ে,
তার জন্য এক জুমা থেকে আরেক জুমা পর্যন্ত নূর দেওয়া হয়।

সূরা কাহফ শুধু ফজিলতের সূরা না।


এটা মানুষকে দুনিয়ার ফিতনা, অহংকার, সম্পদের পরীক্ষা, ক্ষমতার পরীক্ষা—এসব বুঝতে শেখায়।

আর যে মানুষ দুনিয়ার ফিতনা বুঝে,


সে রিজিককেও বেশি পরিষ্কারভাবে বুঝতে পারে।
সে জানে — সবকিছু টাকা না।
বরং হিদায়াত, নূর, সঠিক সিদ্ধান্ত আর মানসিক স্বস্তিও রিজিকের অংশ।

জুমার দিন ফজরের পর,


অথবা জুমার আগে,
অথবা দিনের যেকোনো সুবিধাজনক সময়ে সূরা কাহফ পড়ে নিন।
একবারে না পারলে ভাগ করে পড়লেও হবে।

৩. আসরের পর দোয়া করুন

জুমার দিনে এমন একটি সময় আছে,


যে সময় একজন মুসলিম আল্লাহর কাছে যা চাইবে,
আল্লাহ তা দান করবেন।

অনেক উলামা বলেছেন,


আসরের পর থেকে মাগরিবের আগ পর্যন্ত সময় খুবই মূল্যবান।

এই সময়টাকে হালকাভাবে নেবেন না।


বিশেষ করে যদি আপনার জীবনে রিজিকের কষ্ট, ঋণের চাপ, কাজের টেনশন, সংসারের অশান্তি, বা ভবিষ্যৎ নিয়ে ভয়
থাকে—তাহলে এই সময়টা আপনার জন্য খুব বড় সুযোগ।

এই সময় আপনি নিজের ভাষায় দোয়া করতে পারেন।


এর সাথে এই দোয়াগুলোও পড়তে পারেন—

আরবি:
‫َربِّ ِإ ِّني لِ َما َأ ْن َز ْلتَ ِإ َليَّ مِنْ َخي ٍْر َفقِي ٌر‬

বাংলা উচ্চারণ:
রব্বি ইন্নী লিমা আনযালতা ইলাইয়্যা মিন খাইরিন ফাকীর।

বাংলা অর্থ:
হে আমার রব, আপনি আমার দিকে যে কল্যাণই পাঠাবেন, আমি তার মুখাপেক্ষী।

আরবি:
َ ‫ َوَأ ْغ ِننِي ِب َفضْ ل َِك َعمَّنْ سِ َو‬،‫ِك‬
‫اك‬ ْ ‫اللَّ ُه َّم‬
َ ‫اك ِفنِي ِب َحاَل ل َِك َعنْ َح َرام‬
বাংলা উচ্চারণ:
আল্লাহুম্মাকফিনী বিহালালিকা আন হারামিকা, ওয়া আগনিনী বিফাদলিকা আম্মান সিওয়াক।

বাংলা অর্থ:
হে আল্লাহ, আপনার হালাল দিয়ে আমাকে হারাম থেকে বাঁচান, আর আপনার অনুগ্রহ দিয়ে আমাকে আপনি ছাড়া অন্য সবার
মুখাপেক্ষী হওয়া থেকে মুক্ত করুন।

জুমার দিনের সহজ রুটিন

ফজরের পর —
সূরা কাহফ শুরু করুন বা পড়ে ফেলুন।

সারাদিন —
বেশি বেশি দরূদ পড়ুন।

আসরের পর —
কিছু টা নিরিবিলি হয়ে বসুন।
দোয়া করুন।
রিজিক, ঋণ, চাকরি, ব্যবসা, সংসার, মানসিক স্বস্তি—সব নিয়ে আল্লাহর কাছে খুলে বলুন।

মনে রাখবেন

জুমার দিনের বরকত শুধু নামাজ পড়ে শেষ হয়ে যায় না।
অনেক মানুষ জুমা পড়ে বের হয়ে আবার দুনিয়ার ব্যস্ততায় হারিয়ে যায়।
কিন্তু যে মানুষ এই দিনের লুকানো আমলগুলো ধরতে পারে,
সে সারা সপ্তাহের জন্য এক ধরনের নূর, শক্তি আর বরকত পেতে শুরু করে।

তাই জুমার দিনটাকে শুধু সাপ্তাহিক রুটিন বানাবেন না।


এটাকে বানান রহমতের দিন।
দোয়ায় ভেজা দিন।
আর রিজিকের জন্য আল্লাহর দিকে ফিরে আসার দিন।

— Ekram Hossain - ইকরাম হোসাইন

মনে রাখবেন:
জুমার দিনের সবচেয়ে বড় সৌন্দর্য হলো,
এই দিনে অল্প আমলেও বড় বরকত লুকিয়ে থাকে।
তাই জুমাকে শুধু পালন করবেন না,
জুমাকে অনুভব করুন।
হয়তো এই একদিনের আমলই আপনার পুরো সপ্তাহ বদলে দিতে পারে।

সাওয়াবের নিয়তে পৌঁছে দিন সবার কাছে।


Recipies
links
[Link]

Relational DBMS Course – Database Concepts, Design & Querying Tutorial

Why Cybersecurity Skills Are Important for Front-End Developers

[Link]

Ai Automation Full Course - Zero To Saas Product Building Course

Tasbih Application.mp4

How to Build a Custom PDF Text Extractor with [Link] and TypeScript

How to Build a Résumé Screening System Using Python and Multiprocessing

মন্টু মিয়াঁর সিস্টেম ডিজাইন অভিযান

How to Build an Animated Shadcn Tab Component with Shadcn/ui

What Is Software Testing | Importance of Software Testing | Types of Software Testing | B…


100% কোড AI করে দিবে ! সফটওয়্যার ডেভেলপার ভবিষ্যৎ কি | AI VS Software Development Expl…

Learn Relational Database Design

System Design 01: শূন্য থেকে শুরু করার Complete Guide

How to Use the "this" Keyword in JavaScript: A Handbook for Devs

Build and Deploy a Polished AI Project and Get Sales

Build and Deploy a Polished AI Project and Get Sales

What Are JSON Web Tokens (JWT)?

Mastering C++ STL | Pair ,Tuple, Vector, String, Map | বাংলায় Complete Tutorial

ইংলিশ কোর্স সেলস কনটেন্ট আইডিয়া ও কনটেন্ট প্ল্যানিং গাইড - Content King

BD Big Tech Interview | এই LeetCode 300 Problem + CS 200 Questions যা Cover করলেই হবে

Let’s Talk About Object-Oriented Programming (OOP) — Part 1 | by Minhajul Islam (Minhaj)

They’re Watching You Through Wi-Fi… And You Have No Idea


Learn High-Level System Design by Building a YouTube Clone

Data Structures and Algorithms Mega Course – Master Technical Interviews in 49 Hours

Complete Cybersecurity Roadmap 2026 | ft. Mahmud Rahman (AWS, Ex-Microsoft)

[Link]
Making Software

Free Data Analysis

[Link]
Unreal Engine Official Learning Resources

Stack-O-Bot

Stack O Bot Sample Game in Unreal Engine

Crash Course: Making an Animation


In-Engine with an In-Engine Rig

Crash Course: Making an Animation In-Engine with an In-Engine Rig | Epic Developer
Community

Your First Hour in Unreal Engine 5.2

Your First Hour in Unreal Engine 5.2 | Course

Your First Game In Unreal Engine 5


Your First Game In Unreal Engine 5 | Epic Developer Community

Blueprint Communication

[Link]
ation/8nv8/unreal-engine-level-blueprint

ChodChef 3 Star Coder Github and Codechef Problem solves

👋 👩‍💻
🔥 🥘 ❓,
GitHub - nidhiupman568/Codechef-Problem-Solved: Hi! I'm Nidhi Upman, a passionate

💪 📈 🧠 🌟 🗓️, I
competitive programmer active on CodeChef . Starting with just solving 1️⃣-2️⃣ questions

🏆 🔄 💻
I persisted , upsolving to master and achieve 3-star status. Every Wednesday
tackle contests and daily practice ensures continuous improvement .

Job Math Bangla

[Link]

Job English in Bangla

English Moja - YouTube

Job GK Bangla
প্রাচীন আমল lecture 2 basic view. নাঈম ভাই

Japanese Reading With Manga

Qaantar - YouTube

Sam - [Link]

Manga - [Link]

Blueprint Advance - Unreal Engine 5 | Blueprint For Intermediate Users

NextJs Starter Temp + Backend [Link]

বর্ত মান বিশ্ব রাজনীতি বুঝতে এই ৮টি মৌলিক থিওরি জানা আবশ্যক | কনসেপচু য়াল ইস্যুজ

A, B, C, D, E - Codeforces Round 1080 (Div. 3) Video Solution in Bangla | CPS Academy

Contributing To Open Source – Beginner's Guide

Become an Open Source Master

AI-এর যুগে সফটওয়্যার ইঞ্জিনিয়ারিং: টিকে থাকার এবং এগিয়ে যাওয়ার পূর্ণাঙ্গ গাইড

Master AI Coding: Copy-Pasting বন্ধ করে Engineering করুন (2026 Guide)

Why React Needs Design Patterns + 10-Min Crash Course


Design patterns in React

[Link]

[Link]
ation/8nv8/unreal-engine-level-blueprint
[Link]
[Link]

৬টি গুরুত্বপূর্ণ ভূ -রাজনৈতিক তত্ত্ব | BCS International Affairs | Geopolitics Theories in Bangla

ভূ রাজনৈতিক তত্ত্ব | ডমিনো, হার্ট ল্যান্ড, ট্রু ম্যান ডকট্রিন, মার্শাল প্ল্যান | আন্তর্জ াতিক বিষয়াবলী
AI Courses
[Link]

[Link]

[Link]

AI-Assisted Coding Tutorial – OpenClaw, GitHub Copilot, Claude Code, CodeRabbit, Gem…

MCP সার্ভ ার কি? সহজ কনসেপ্ট ও TypeScript SDK উদাহরণ

Build & Monetize Your First MCP Server (Bangla) | MCPize Tutorial

Deploying AI Models with Hugging Face – Hands-On Course


Ai Automation Full Course - Zero To Saas Product Building Course

AI Foundations for Absolute Beginners

how-to-build-a-secure-ai-pr-reviewer-with-claude

Automate GitHub PR Reviews with AI ([Link], Claude & GitHub Actions)

The AI in Healthcare Handbook: Intelligent Care from Lab to Clinic

Claude Code Essentials

AI Dictionary for Beginners


Articles and blogs
[Link]
Coding Stuff
Simple Tips to Help You Write Clean Code
Books
ফ্যান্টমস অব চিটাগাং | মেজর জেনারেল এস এস উবান | দ্য ফিফথ আর্মি ইন বাংলাদেশ | হায়রে অকৃ তজ্ঞ জাতি !

Physics Guide : [Link]

[Link]

কেন উর্বর জমি হয়েও বাংলাদেশ আজও গরিব? | The Delta Paradox | Boi Kotha Koy

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]

[Link]
[Link]

[Link]
Short Reads
[Link]

[Link]
Comics
Power Fantasy

[Link]
Shows
About the Movement of the Earth - E11

Monster- E25

Code Geass: Lelouch of the Rebellion- E15

To Be Hero X-E18

Mission: Yozakura Family-E19

100 Meters

Solo Leveling-3
Future Employment Research
Future Employment Research

Step-by-Step Guide to Start Data


Annotation
1️⃣ Build the Minimum Skills (1–4 weeks)
You do NOT need to be a programmer, but you must be detail-oriented.

Focus on:

✅ Good English reading comprehension​


✅ Basic computer skills​
✅ Fast and accurate mouse/keyboard usage​
✅ Ability to follow strict instructions
BONUS (highly recommended):

●​ Learn basic image labeling concepts


●​ Bounding boxes
●​ Segmentation
●​ Text labeling
●​ Audio transcription

👉 You can learn these in a few hours on YouTube by searching:​


“data annotation tutorial” or “image labeling tutorial.”

2️⃣ Apply to Legit Platforms (Very Important)


Avoid Facebook scams — many fake “AI job” ads target Bangladesh.

🌎 International Platforms (Remote Friendly)


Apply to several — acceptance is not guaranteed.

👉
●​ Remotasks → Very popular in South Asia​
[Link]
👉
●​ Toloka (by Yandex) → Beginner friendly​
[Link]

👉
●​ Appen → More professional projects​
[Link]

👉
●​ TELUS AI (formerly Lionbridge)​
[Link]

👉
●​ Clickworker​
[Link]

👉 Apply to at least 3–4. Don’t wait for one.

3️⃣ Expect a Qualification Test


Almost every platform will test you.

Common topics:

●​ Identify objects in images


●​ Categorize text
●​ Judge search results
●​ Audio transcription

💡 Tip:​
Read instructions VERY carefully — most people fail because they rush.

4️⃣ Realistic Income Expectations (Bangladesh)


Let me be honest here 👇
Beginner:

👉 8,000 – 20,000 BDT/month (part-time level effort)


Intermediate:

👉 20,000 – 45,000 BDT


Skilled / high-accuracy workers:

👉 50k+ BDT possible


But this is NOT guaranteed stable salary — it depends on project availability.

Think of it as:

👉 freelancing-style digital labor.

⚠️ Things People Don’t Tell You


❌ It can be repetitive
❌ Work may suddenly stop
❌ Some tasks pay very low
So do NOT rely on it as your only long-term plan.

⭐ SMART Strategy (Highly


Recommended)
Instead of staying an annotator forever:

👉 Enter annotation → Learn → Move up.


After 6–12 months try to transition into:

●​ AI data quality analyst


●​ Prompt engineering
●​ AI testing
●​ QA roles
●​ Basic Python + ML datasets

These pay MUCH better.

🚨 My Strong Advice (based on market trends)


Data annotation is best if you are:

✅ Student​
✅ Job searching​
✅ Want remote income​
✅ Switching careers​
✅ Entering tech without coding
But long-term — skill upgrade is mandatory.
Git Links
steipete (Peter Steinberger) · GitHub
JOB Forms
Monipuripara,gate-3,house-25

TEJGAON

1215

DHAKA

NID - 6005202566

SSC- 5.00 - 125241

HSC - 4.83 - 125240

BSc - 3.65
Mobile Service
[Link]

You might also like