Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Find Four Elements That Sums To A Given Value
MEDIUM
10 mins
82 upvotes
Sorting
Arrays
Binary Search
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Sorting
-
-
Binary Search
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Find Four Elements That Sums To A Given Value

Contributed by
Priyaraj
Medium
yellow-spark
0/80
Avg time to solve 10 mins
Success Rate 90 %
Share
82 upvotes

Problem Statement

You are given an array/list 'ARR' of ‘N’ integers and an integer value ‘TARGET’. You need to check whether there exist four numbers (ARR[i], ARR[j], ARR[k], ARR[l]) such that (0 <= i < j < k < l < N) and ARR[i] + ARR[j] + ARR[k] + ARR[l] = 'TARGET'.

Note:
1. All four numbers should exist at different indices in the given array.
2. The answer is case-sensitive.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
5 9
1 3 3 10 2
6 20
2 4 6 3 1 1
Sample Output 1:
Yes
No
Explanation For Sample Input 1:
Test case 1:
The elements at indices (0, 1, 2, 4) gives sum 9 i.e, ARR[0] + ARR[1] + ARR[2] + ARR[4] = 9. Hence the answer is Yes.

Test case 2:
None of the combinations of 4 numbers gives 20 as 'TARGET'. Hence the answer is No.  
Sample Input 2:
2
5 15
0 10 1 2 2
6 20
-2 12 -1 1 20 1 
Sample Output 2:
Yes
Yes
Reset Code
Full screen
Auto
copy-code
Console