Four Sum Problem (4sum) Solution in Java, Python, C++

Given an array and K, your task is to find four elements in an array such that their sum is equal to K.
You have to print all four elements in lexicographical order and make sure that no pair get repeated.

(Level: Hard)
For Example: 
N = 9 and array = [3, 6, 8, 2, 3, 9, 5, 1, 7] and K = 18
so, one pair is 1+3+5+9 = 18 you have to find all pairs

A SOLUTION IN C++: You can optimize it further!



A SOLUTION IN PYTHON : 



AN OPTIMIZED SOLUTION IN JAVA : 



Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment.

To solve more problems on Array and String, please check out these articles "Top 10 Array Interview Questions" and "Top 10 String Interview Questions".

All the Best!

Comments

Popular posts from this blog

Find the length of the longest substring without repeating character

Reverse Words in a Sentence Without using any Library