1. Two Sum — Leetcode — JavaScript Solution using for loops - by Abu Saleh Faysal

1. Two Sum — Leetcode — JavaScript Solution using for loops - by Abu Saleh Faysal

Table of contents

No heading

No headings in the article.

1. Two Sum

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.

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

Solution:

tt4p5qome1ic6atin400.png

Step 01: I simply iterate the array using for loops, and inside the for loops, I iterate the array again but this time start the index with the next position(compare to the first iteration)

Step 02: In this way, I got pair of numbers from the array and check if the total of the pair is equal to the target number or not.

Step 03: If the total value is equal to the target, I simply return the index numbers.

Video Tutorial

If you want me to publish more posts like this, Buy me a coffee

👉 YouTube Channel Link: youtube.com/channel/UCW_09Nbobf4URLkAlEo84sw

👉 PlayList Link: youtube.com/playlist?list=PLUnklBXn8NSefCpB..

👉 Connect with me (LinkedIn): linkedin.com/in/abusalehfaysal

👉 Follow our LinkedIn Page: linkedin.com/company/thebacklogprogrammer

👉 Like our Facebook page: facebook.com/thebacklogprogrammer

👉 Join our community (Facebook group): facebook.com/groups/5500588936676942

👉 Follow me at: facebook.com/AbuSalehFaysal10

👉 Twitter: twitter.com/AbuSalehFaysal

👉 Abu Saleh Faysal’s Blog: abusalehfaysal.hashnode.dev

👉 Hasnode: hashnode.com/@AbuSalehFaysal

👉 Dev Community: dev.to/abusalehfaysal

👉 freeCodeCamp: freecodecamp.org/abusalehfaysal

👉 Medium: abusalehfaysal.medium.com

👉 GitHub: github.com/AbuSalehFaysal

👉 GitLab: gitlab.com/AbuSalehFaysal