Mohit Profile picture
Jun 19 β€’ 7 tweets β€’ 2 min read
Day 2 of #100DaysOfCode

PROBLEM NAME - 3 SUM

Given an array of integers "nums" and an integer "target", find all possible indices of triplets such that they add up to target.

A thread πŸ§΅πŸ‘‡
Approach - 1 : Brute Force

Use 3 "for loops" and check for all possible triplets of numbers that makes the target sum.

Time Complexity - O(n^3)
Space Complexity - O(1) Image
2nd and 3rd approaches requires knowledge of HashMap and 2 pointers that I discussed in the 2SUM problem in my previous thread.

Here is the thread in which I covered those topics so give it a read before proceeding further!

Approach - 2 : Using HashMap

- Go to each element of the array and use the HashMap Approach to find the pair of 2 numbers from the rest of the array.

- Refer to my Day 1 thread to know more about the HashMap Approach

Time Complexity - O(n^2)
Space Complexity - O(n)
Approach - 3 : Sorting + 2 pointers (Most Optimal)

- Sort the array

- Go to each element of the array and use the 2 pointers approach to find the other pairs from the rest of the array.

- Refer to my Day 1 thread to know more about the 2 pointers approach.
Time Complexity - O(n^2)
Space Complexity - O(1) Image
I hope you guys understood all the approaches to solve this problem! Consider retweeting if it helped.

I will be sharing much more problems in the upcoming days so make sure you follow along @_MohitBalwani !

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Mohit

Mohit Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @_MohitBalwani

Jun 21
Day 4 of #100DaysOfCode :

Problem Name - Longest Consecutive Sequence

Given an unsorted array of integers "nums", return the length of the longest consecutive elements sequence.

πŸ§΅πŸ‘‡
Sample Input :

arr = [100,4,200,1,3,2]

Sample Output:

4

Explanation:

The longest consecutive elements sequence is [1, 2, 3, 4].

Therefore its length is 4.
Below is the link to that problem, you can try solving it on your own.

leetcode.com/problems/longe…

If you are unable to solve, here is the detailed explanation!
Read 8 tweets
Jun 20
Day 3 of #100DaysOfCode :
Problem Name - Longest Mountain in Array

Given an integer array "arr", return the length of the longest subarray, which is a mountain.

πŸ§΅πŸ‘‡
Sample Input :

arr = [2,1,4,7,3,2,5]

Sample Output:

5

Explanation:

A mountain is a set of numbers that are strictly increasing until they reach a peak, at which they become strictly decreasing.

So, the largest mountain is [1,4,7,3,2] with peak at 7 and length 5.
Here is the link to that problem, you can try solving it on your own.

If you are unable to solve, here is the detailed explanation!

leetcode.com/problems/longe…
Read 6 tweets
Jun 18
Day 1 of #100DaysOfCode :
Problem Name - 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.

A thread πŸ§΅πŸ‘‡
Approach - 1 : Brute Force

- Use 2 for loops and check for all possible pairs of numbers that makes the target sum.

Time Complexity - O(n^2)
Space Complexity - O(1) Image
Approach - 2 : Sorting + Two Pointers

- Consider nums = [2,15,11,7] and target = 9

- After Sorting, nums = [2,7,11,15]

- Take 2 pointers, "start" and "end" pointing to the first and last element of the array.

- Let, sum = nums[start] + nuts[end]
Read 8 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(