Queue implementation - C++

We talked about queue in this post. Now, I’m gonna explain how to implement this data structure. What we need is a basic understanding of pointer and memory. Queue has 4 main methods size() - returns the number of the elements top() - returns the first element of the queue pop() - deletes the first element push(element) - insert new element back of the queue How hard can it be?...

September 24, 2023 · 4 min · 729 words · Me

Memory Management - PART 1

Memory and pointer We learned how to declare variables array, vector etc. But how do these things works under the hood? Lets start with memory. All the are stored in memory. For example if we declare array or vector, c++ will allocate new memory, which we can then use directly. If we no longer need these variable, they will be cleaned up automatically, like variables declared within a function. But, how do we know where they are stored?...

September 2, 2023 · 3 min · 476 words · Me

Prime numbers

Prime number A prime number is a natural number with exactly 2 distinct divisors. For example 2, 3, 5, 7 and so on. Given number N, How do we determine if it is prime? One approach is to iterate through the numbers [1, N] and count how many of them divide N. Can we check it faster than this? If prime number has only 2 divisors, that means it s divisible by only 1 and itself....

June 20, 2023 · 3 min · 457 words · Me

Union Find

About the Disjoint Set Union (Union Find) data structure: Judging by its name, the data structure consists of three words: union, find, and disjoint. There are three main operations or concepts associated with it. Determining to which part it belongs Union, which involves combining two separate parts. The structure has two actions. Initially, we start with multiple separate parts and the goal is to connect them easily while being able to determine to which part it belongs....

May 14, 2023 · 4 min · 698 words · Me

Graph

What is graph Wikipedia: A graph is a structure made of vertices and edges. I: Ok A graph can be described as a way of representing relationships between things. For example, let’s consider the relationship of friends. A is a friend of B, B is a friend of C, and C is a friend of D. Additionally, D is a friend of A, and B. In this scenario, we refer to these individuals as vertices, and the connections between them as edges....

May 13, 2023 · 9 min · 1815 words · Me

Binary search

Binary search algorithms are everywhere, and for good reason. They embody a simple yet powerful idea. In general, algorithms are named with purpose. Let’s take a closer look at this algorithm through an example problem. Problem We are given an ascending sequence of length N and Q requests. Each request consists of a number, and the answer indicates whether that number exists in the sequence. 1 <= N, Q <= 10^5, abs(a[i]) <= 10^9....

May 11, 2023 · 3 min · 525 words · Me

Power of number

Problem Find the answer obtained by raising A to the power of B, modulo the prime number 10^9+7. Here, A and B are both less than or equal to 10^13. We can find this using a simple loop. In other words, the number A is multiplied by B times, and we are talking about 10^13 times. How much action is that in general? A lot, and if you imagine that 300 million operations are performed in 1 second, then it will work for more than 9 hours....

April 15, 2023 · 5 min · 949 words · Me

Basic data structure

C++ has many simplified data structures that we don’t need to write ourselves, and let’s talk about them. At first, I will write about what they do, what their structure is, and then I think I will write the code for all of them myself and how they work. Array About Array. An Array can be thought of as a container that can hold many things. Currently we only store 1 value in 1 variable, so what if we need to store 2000 variables?...

April 4, 2023 · 9 min · 1769 words · Me

Recursion

About recursion. We have seen about functions in previous post is. So what happens if the function calls itself? This is called a recursive function. For example int sum(int a, int b) { return sum(a, b); } Let’s look at the function. sum takes 2 variables and returns an int value. But what will happen if he called himself inside? Of course it will run indefinitely and I certainly don’t want that....

March 3, 2023 · 2 min · 394 words · Me

Step by Step Programming - C

How to learn programming? In my experience, I have familiarized myself with the basics of programming and understand the fundamental concepts, then I start solve competitive programming problems. To begin with, it’s recommended to focus on learning the fundamentals of C and C++ programming languages, as they provide a solid foundation. As I mentioned earlier, problem-solving skills are paramount. Platforms like Hackerrank, Codeforces, and Codechef are excellent resources to enhance your problem-solving abilities....

February 2, 2023 · 11 min · 2310 words · Me