I solved my first Array Question on Leetcode🎉

I solved my first Array Question on Leetcode🎉

"It always seems impossible until it's done." - Nelson Mandela

Introduction

Getting back to the proper study of Data Structures and Algorithm after a long time was a discouragement on its own but I am always motivated by the end goal.

Recently, I started my journey of Data Structures and Algorithm with Arrays and I plan to solve as many problems as I can solve on Leetcode because I believe that solving those problems will make me have good understanding of these data structures concept I am learning.

Solving my first problem on Arrays was like an impossible feat but I did it with determination and a lot of failed attempts.

In this article I will take you through the question and how I came about with the solution to this question.

Prerequisite

To get the best out of this article you will need to be comfortable with the basics of a programming language and I will recommend JavaScript because I used JavaScript to solve this problem.

Another requirement to get the best out of this article is an open heart to learn✨

Goal of this Article

  1. Every reader of this article should be able to solve this question anytime they come across it anywhere.
  2. This article is also aimed at motivating someone to go on Leetcode and solve their first Data Structures problem too.

Array Question

The topic of the question I solved is Two Sum which is under the problems page on Leetcode, the question is Easy but requires you to think. There are a lot of solutions to it but you know that we are talking about becoming a good programmer and this depends on the time and space complexity of your solution.

Question

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

And the following examples were given:

Examples 1

Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Examples 2

Input: nums = [3,2,4], target = 6 Output: [1,2]

Examples 3

Input: nums = [3,3], target = 6 Output: [0,1]

Solution

With the information in the previous section we should be able to approach it and solve it. I will first explain the concept behind the solution and we will see a code snippet showing the solution.

Steps to Follow

I believe we know how to create a for loop in JavaScript, because that is what we will use in this solution and if you want to check up on it you can check it up here.

We will create two for loops, the first one is to loop through the nums array the first time and the second one is to loop through it the second time. After looping through the array twice then we can use a conditional statement to check if any value in these two loops can sum up to our target which is our goal without using the same element twice.

To understand better, the code snippet explains what I am trying to say

Step 1

Create a Function and name it twoSum then pass the nums array and target as an argument

var twoSum = function(nums, target) {
};

twoSum([2,7,11,15], 9);

Step 2

Loop through the array the first time using for loop

var twoSum = function(nums, target) {
    for (let i = 0; i < nums.length; i++){
    }
};

twoSum([2,7,11,15], 9);

Step 3

Loop through the array the second time using for loop also.

var twoSum = function(nums, target) {
    for (let i = 0; i < nums.length; i++){
        for (let j = 0; j < nums.length; j++){
            }
        }
    }
};

twoSum([2,7,11,15], 9);

Step 4

Use a conditional statement to check the values of our loop which in this case it is nums[i] and nums[j] to see if the values sum up to the target and if it is, we will then check if it is not the same value by doing i != j

var twoSum = function(nums, target) {

    for (let i = 0; i < nums.length; i++){
        for (let j = 0; j < nums.length; j++){
            if ((nums[i] + nums[j]) == target && i != j){
            }
        }
    }
};

twoSum([2,7,11,15], 9);

After all these has been done, we will then return the array of the index of these two values that gives us the sum of the target.

var twoSum = function(nums, target) {

    for (let i = 0; i < nums.length; i++){
        for (let j = 0; j < nums.length; j++){
            if ((nums[i] + nums[j]) == target && i != j){
               return [i, j];
            }
        }
    }
};

twoSum([2,7,11,15], 9);

This is one of the many methods used because there are a lot of methods you can use and the purpose of this article is to share the method I used myself.

The Time and Space Complexity of my method

This method I used has a runtime of 144ms and a space of 39.6mb in memory. This is not the best solution because there a lot of better solution that took lesser time and smaller space. I believe in consistency and improvement and I know that I will get better at it with constant practise😊

dsa-question-1-extra.JPG

Conclusion

The summary of what I will like to say is that keep practicing, the secret to getting better is doing it consistently and I have decided to do it consistently.

I hope you have learnt a lot from this article and your contribution is also highly welcomed because we get better by learning from others, you can reach out to me on Twitter here . Watch out for more helpful contents on Data Structures and Algorithm from me.

Don't forget to Like, share and subscribe for more articles in this series called Getting Started with Data Structures and Algorithm by The Code Lord❤

Enjoy 🎉