Блог пользователя singlabharat

Автор singlabharat, история, 4 месяца назад, По-английски

Hello CF!

Here's a list of the DSA, in the order I would encourage you to learn them, with some gem tutorials for each:

1) Time Complexity

2) Sorting

3) Binary Search

4) 2 Pointers & Sliding Window

5) Recursion

6) Bit Manipulation

7) Number theory

8) Dynamic Programming

9) Trees & Graphs

10) UFDS/DSU

11) Segment Trees

This is some of the best content out there that I've myself learnt from, taught, and discovered over the years...

Hope you find them useful!

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

Автор singlabharat, история, 3 года назад, По-английски

Hello CF!

Let me ask you, what is 3 & 6 (where & is the bitwise AND operator)?

You'll answer 2, right? Well, I wrote this piece of code (don't remember why)

if (3 & 6 == 2) cout << "Equal";
else cout << "Not Equal";

...and the output is

Spoiler

What in this universe happened??? Though this might seem obvious to some, it potentially could cost you a whole problem... Let me briefly explain the "why" so you don't make the same error I made.

The reason is Operator Precedence

Counter-intuitively, operators like &, ^, | should all have a higher precedence than == in C++, turns out this is not the case!

Have a look here

Thus the above code actually is if (3 & (6 == 2)) which is if (0)

So, the takeaway is to always wrap bitwise operations in parenthesis to avoid such mistakes.

Here's the correct code, which gives the desired output "Equal"

if ((3 & 6) == 2) cout << "Equal";
else cout << "Not Equal";

Hope this helps!

Полный текст и комментарии »

  • Проголосовать: нравится
  • +38
  • Проголосовать: не нравится

Автор singlabharat, 3 года назад, По-английски
A month ago
Spoiler
A week ago
Spoiler

Cud someone pls explain the context as to why this happened?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -36
  • Проголосовать: не нравится