Triplet Sum in Array | GFG | Geeks for Geeks | Problem Solving | Hindi | FAANG | Shashwat
Vložit
- čas přidán 4. 07. 2021
- Java Plus DSA Complete Placement Course:
• Java and DSA Course Pl...
Problem Statement:
Given an array arr of size n and an integer X. Find if there's a triplet in the array which sums up to the given integer X.
Problem Link:
practice.geeksforgeeks.org/pr...
Company Tags:
Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe
Instagram Handle: (@shashwat_tiwari_st)
shashwattiwari.page.link/shas...
#ShashwatTiwari #coding #problemsolving #leetcode #hackerrank #hackerearth #codechef #codesignal #algorithms #javaprogramming #sde #placement #programming
This is a very old video, for better sound and camera quality DSA videos, learn from the below playlist!
Java Plus DSA ( Java + DSA + Problem Solving )
czcams.com/play/PLQ7ZAf76c0ZPVdhV1bAjFv0bQc1xHURzE.html
One of the most underrated channels on youtube , love your videos .
sir your explaination style is so good
I thought of the hashset approach initially but it uses n space so I think sorting is a better approach - Also in GFG its said to solve in const. space
love from upes college bhaiya
Nicely explained
Nice Explanation
Nicely explained 👍
hello shashwat bhaiya me too from upes dehradun
if the values are repeated then? for example 1,1,5,5,7,7,7,8 and we need number of pairs having 12 sum
But the example you gave, there are no Triplets that will sum up to 12.
But to answer your question, repetition won't be a problem.
Question: 5,5,5,7 and sum is 15.
Try this question using map method which I told. You will get your answer
@@shashwat_tiwari_st yes sorry I forgot to mention ...only 2number pair
@@abhishekchakrabarty2930
This is a different question check here: czcams.com/video/0VHC1KVHH0U/video.html
Hey bro if we have question where we need to find number of triplet in the array with given sum then we can't do using hashing right ?
Yes, correct.
@@shashwat_tiwari_st thanks bro.
but exits method will do iteration under hood,
Hey, why did you declare the hasmap inside the first loop and not outside.
same doubt
@@rocked789 I dry ran it and understood why he did that
@@gtx1650max-q can u please explain?
@@rocked789 it's tough to explain on comments. You should try dry run his code yourself I am sure you will get it
In HashMap solution,why the first for loop is running till n-2 ?
Index n-2 is exclusive. ( < sign is used )
Index 0 to Index n-3 will be the possible values for the first element, why it is so Because if we choose 2nd last or last element as the first candidate, we will not have enough elements left to make a triplet.
So, if array is
2, 3, 4, 6, 8.
First elements can be 2, 3, 4 only.
If we choose 6 as first element.
Then 8 Will be the second element,
But no element is left now so we will not be able to choose 3rd element, hence triplet will be incomplete.
@@shashwat_tiwari_st Thank you so much ♥♥