Introduction to Problem Solving

Hi All, This is the first article in our series of the conquer the competitive coding. In this article, we will get a basic introduction about problem-solving and how to build intuition for any problem

What is an Algorithm?

We all know that a well-defined sequence of steps to perform any task is an algorithm, right!!

Suppose lets someone asks you to make a maggie so you will do below mentioned steps.

  1. Check if Maggie is available.
  2. Go to the kitchen, take a pan and pour water, and put it on gas.
  3. Add maggie when water is hot and wait till Maggie is ready.

So we can say that the above is the algorithm for making maggie.

Now suppose you have a new problem for which you don't know the solution then what you will do?

You will try to connect that problem with other problems you already know or you will try to divide that problem into subproblems that are already known to you. So below are the two main tips which you need to remember to become a good problem solver.

  1. Start making observations
  2. Divide the problem into smaller subproblems.

Let's take one simple problem of adding numbers from 1 to N.

One brute force approach to solving this question can be to run a loop from 1 to N and add it. now let's try to make observations for N = 10

S = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
Here we can see that 
9 + 1 = 10
8 + 2 = 10
7 + 3 = 10 and so on...
So we observed that to take sum from 1 to N there will be always exists 
some N - 1, for which N - 1 + 1 = N, N - 2 + 2 = N and so on..

So, therefore

S =      1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
S = 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 
-------------------------------------------------
2S = 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 (11 times 10)

2S = 11 * 10
S = 11 * 10/2

So we came to know that for any sequence from 1 to N 
if we add it twice there will be N + 1 pair which will give us sum = N.

So sum from 1 to N = N * (N + 1)/2

So we can also get the sum of 1 to N in constant time by 
using the above formula.

Don't feel discouraged if you are not able to build the intuition for the above problem as this is just the introduction article. As we will go ahead in this series you will be confident in problem-solving and building intuition.

We will an article per week in this series and we will discuss on below topics.

  1. Introduction to Time Complexity
  2. Introduction to number system and bit manipulation
  3. Array and multidimensional Array
  4. Hashing and problems on hashing
  5. Strings, Sortings, Pointers, and Problems

I will use Python as the language for problem-solving. You can use your favorite programming language. I will also share coding question assignments at end of every article.

There is one famous quote in Gujarati.

"થોડું કરો પણ તમે જે કરો તે નિયમિત કરો" ("Do little but whatever you do do it regular")

Let me know your views and questions in a comment. Happy to connect and learn together.