Day 2: I4G 10 days of code challenge.

Day 2: I4G 10 days of code challenge.

An explanation on what algorithms are and an example on how they are used to solve problems (not world hunger but close).

TL;DR: Your attention span will increase by 30% if you read through the entire article🐱‍👤.

INTRODUCTION

The Day 2 challenge on Leet Code read "Remove Element".

In this article, I try to make that sentence sound a lot less vague and also explain how I solved this challenge eventually. (Actually took me less than 3 minutes😌, but who is counting?).

It is then only timely to ask; what are algorithms?

ALGORITHMS 🤖

Simply put, algorithms are finite and precise steps that solve a problem. Sometimes, it just describes a computational procedure or method. Yes, it is very similar to what was taught in all those computer classes we didn't exactly like (we = I).

How do algorithms work in data structures then? To answer this, we'll take a brief look at what data structures are first. Data structures are ways of organising data in a computer so that it can be used efficiently. Thus, the purpose of algorithms in the context of data structures is to outline steps in efficiently accessing or performing functions on a group of data.

An example would be adding a number "1" to all the numbers in an array (arrays are a form of organising and storing data. Which is the same as? Data structures! ). In this example, we could detail the steps of adding 1 to all those numbers. Then, we would simply be writing an algorithm.

To wrap this all up, it might be fun to note that the word algorithm came from the name of an Arabic man called "Al-Khwarizmi"

THE CHALLENGE

The challenge from Leet Code was to remove a particular value from an array (of numbers in this case). A more interesting example: Let's say you own a kindergarten school admitting toddlers from the ages of 2 to the ages of 6. Then, you decide to only start admitting from 3 to 6 (I don't know why you would do such a thing!). When you make this decision, you would have to delete all the 2-year-olds from the current database (an array of ages)

You'd go from [2,2,3,4,4,4,2,6,5,5,2,3,6] to just having [3,4,4,4,6,5,5,3,6] (no more annoying two-year-olds!). The problem becomes how to instruct your computer to do this. Let's find out.

(You can pause here and try it out yourself)

SOLUTION

This was pretty similar to the Day 1 challenge, so it didn't take me much time. The first thing you would do is to create two pointers. We'll call one "fast (f)" and the other "slow (s)". Then, you will have to initialise them to 0. By initialising, we mean placing them at some point in the array.

These pointers are like your workers. Their job is to scan through the array and perform some function for you within it.

In this case, we place them at 0. 0 signifies the first element of an array, 1 signifies the second element and n signifies the (n+1)th element. You get the gist. Both pointers are on the first element of the array now (place 0).

image 6.png

The next thing you should do is to make each pointer do something with each movement. The "f" checks if the number it is positioned on is equal to 2. If it is, f skips ahead doing nothing. Else ( if it is not equal to 2 ), f tells "s" to change the number "s" is positioned on to the "not 2" number. (Yes, you are allowed to read through that again🙂)

After every pointer performs a function, it moves to the next number (or element). "s" doesn't move unless it changes the number it is positioned on. In this case, the first number f and s are positioned on, is 2.

f asks; is 2 = 2? Yes. So, it moves on to the next and s stays the same.

image 5.png

f is now at another 2. It asks; is 2 = 2? Yes. It moves on.

image 8.png

f is at a 3. Is 3 = 2? Nope. s replaces the 1st number (where it is at) with 3 and moves to the next. f also moves on.

image 7.png

Full Circle!

You can look at more challenges from leetcode.com/problems .