-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTripletsWithSumBetweenGivenRange.cpp
More file actions
67 lines (51 loc) · 1.35 KB
/
TripletsWithSumBetweenGivenRange.cpp
File metadata and controls
67 lines (51 loc) · 1.35 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Given an array of real numbers greater than zero in form of strings.
Find if there exists a triplet (a,b,c) such that 1 < a+b+c < 2 .
Return 1 for true or 0 for false.
Example:
Given [0.6, 0.7, 0.8, 1.2, 0.4] ,
You should return 1
as
0.6+0.7+0.4=1.7
1<1.7<2
Hence, the output is 1.
O(n) solution is expected.
Note: You can assume the numbers in strings don’t overflow the primitive data type and there are no leading zeroes in numbers. Extra memory usage is allowed.
LINK: https://www.interviewbit.com/problems/triplets-with-sum-between-given-range/
*/
int Solution::solve(vector<string> &A)
{
long double a,b,c;
a = stold(A[0]);
b = stold(A[1]);
c = stold(A[2]);
for(int i=3;i<A.size();i++)
{
if((a+b+c)>1 && (a+b+c)<2)
return 1;
//cout<<a<<" "<<b<<" "<<c<<"\n";
if((a+b+c)>=2)
{
if(a>b && a>c)
a=stold(A[i]);
else
if(b>a && b>c)
b=stold(A[i]);
else
c=stold(A[i]);
}
else
{
if(a<b && a<c)
a=stold(A[i]);
else
if(b<a && b<c)
b=stold(A[i]);
else
c=stold(A[i]);
}
}
if((a+b+c)>1 && (a+b+c)<2)
return 1;
return 0;
}