1
// This code is using 2nd Approach.
2

3
class Solution {
4
public:
5
string optimalDivision(vector<int> &nums) {
6
int n = nums.size();
7
string ans;
8

9
// check for size if its 1 then we can't use parantheses and
10
// if its 2 then also we should not use it.
11

12
if (n == 1) return ans = to_string(nums[0]);
13
if (n == 2) return ans = to_string(nums[0]) + "/" + to_string(nums[1]);
14

15
// for size greater than 2 add paranthese after first number
16

17
ans = to_string(nums[0]);
18
ans.append("/(");
19
for (int i = 1; i < n - 1; i++) {
20
ans.append(to_string(nums[i]) + "/");
21
}
22
ans.append(to_string(nums[n - 1]));
23
ans.append(")");
24

25
// finally this becomes as a/(b/c/d/....) which is our answer
26

27
return ans;
28
}
29
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0