-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindTripplet.js
35 lines (34 loc) · 894 Bytes
/
FindTripplet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Find a triplet that sum to a given value
Sample:-
Input: arr =[12, 3, 4, 1, 6, 9], value = 24;
Output: [12,3,9];
*/
findTripplets([12, 3, 4, 1, 6, 9],24);
function findTripplets(arr, value){
if(arr.length === 0 || value === undefined){
return new Error('Invalid input');
}
var sol=[];
var tot = arr.length;
for(var i =0;i<tot;i++){
var firstElement = arr[i];
//if firstElement greater than value
if(firstElement > value){
continue;
}
for(var j=i+1;j<tot;j++){
var secondElement = arr[j];
var sum = firstElement+secondElement;
//if first two elements sum greater than value
if(sum > value){
continue;
}
var remain = value-sum;
var thirdElementPosition = arr.indexOf(remain);
if(thirdElementPosition !== -1 && thirdElementPosition > j){
sol.push([firstElement,secondElement,remain]);
}
}
}
return sol;
}