Arrays-Target Sum Triplets Problem


my code is failed in test case 1.

Hi
Your sample test case itself is not giving right output
1, 2 and 7
1, 3 and 6
1, 4 and 5
2, 2 and 6
2, 3 and 5
3, 2 and 5
3, 3 and 4
4, 2 and 4
5, 2 and 3
It is considering duplicates as well,also it is taking all triplets regardless they have been considered before or not. Can you try using hashmaps? Try brainstorming the approach using hashmaps, if you are not able to get clarity, let me know

I am not getting what you are telling.
what is hashmaps and brainstroming approach.

Brainstorming means thinking of an approach with all your mind and soul, it is not an approach :sweat_smile:
Okay so, using hashmaps or set(both are part of STL), you can solve this prob in O(n^2).I am saying to use set/hashmap to avoid scenario in which duplicate triplets are considered. You can also use sorting to solve it in the same time complexity
It would be great if you could code it and then send me, else I will have to tell you entire approach here.


this is the code but it show duplicate triplets

Hi
You have used int l=1, why always start with 1,
you should start l with first+1, to avoid duplicates

Thanks, got it :relaxed:

1 Like