-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compare two fractions.cpp
52 lines (48 loc) · 1.11 KB
/
Compare two fractions.cpp
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
// Time complexity - O(N)
// Space complexity- O(1)
class Solution {
public:
string compareFrac(string str) {
vector<int> v;
int num=0;
for(auto& it:str)
{
if(it=='/' or it==',')
{
v.push_back(num);
num=0;
}
else if(it==' ')
num=0;
else
{
num*=10;
num+=(it-'0');
}
}
v.push_back(num);
double a=(double(v[0]))/v[1];
double b=(double(v[2]))/v[3];
if(a>b)
{
string temp1=to_string(v[0]);
string temp2=to_string(v[1]);
string ans="";
ans+=temp1;
ans+="/";
ans+=temp2;
return ans;
}
else if(b>a)
{
string temp1=to_string(v[2]);
string temp2=to_string(v[3]);
string ans="";
ans+=temp1;
ans+="/";
ans+=temp2;
return ans;
}
return "equal";
}
};