File tree Expand file tree Collapse file tree 2 files changed +107
-0
lines changed Expand file tree Collapse file tree 2 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < sstream>
3+ #include < vector>
4+ #include < algorithm>
5+ #include < set>
6+ #include < map>
7+ using namespace std ;
8+
9+ class AddMultiply {
10+ public:
11+ vector <int > makeExpression (int y)
12+ {
13+ vector<int > v (3 );
14+
15+ for (int i = -1000 ; i <= 1000 ; i++)
16+ for (int j = -1000 ; j <= 1000 ; j++)
17+ for (int k = -1000 ; k <= 100 ; k++){
18+
19+ if (i && j&& k && (i != 1 ) && (j != 1 ) && (k != 1 )){
20+ if ((i*j + k) == y){
21+ v[0 ] = i;
22+ v[1 ] = j;
23+ v[2 ] = k;
24+ return v;
25+ }
26+ }
27+
28+ }
29+ }
30+
31+
32+ };
33+
Original file line number Diff line number Diff line change 1+ PROBLEM STATEMENT
2+ You are given an int y.
3+ We are looking for any vector <int> x that satisfies the following constraints:
4+
5+ x has exactly three elements
6+ ( x[0] * x[1] ) + x[2] = y
7+ Each x[i] must be between -1000 and 1000, inclusive.
8+ No x[i] can be equal to 0 or 1.
9+
10+ Find and return one such x.
11+
12+ If there are multiple valid solutions, you may return any of them.
13+ You may assume that for our constraints on y (specified below) at least one valid x always exists.
14+
15+ DEFINITION
16+ Class:AddMultiply
17+ Method:makeExpression
18+ Parameters:int
19+ Returns:vector <int>
20+ Method signature:vector <int> makeExpression(int y)
21+
22+
23+ CONSTRAINTS
24+ -y will be between 0 and 500, inclusive.
25+
26+
27+ EXAMPLES
28+
29+ 0)
30+ 6
31+
32+ Returns: {2, 2, 2 }
33+
34+ 2*2 + 2 = 6
35+
36+ Note that this is one of many possible solutions. Another solution is:
37+
38+ 3*3 + (-3) = 6
39+
40+
41+ 1)
42+ 11
43+
44+ Returns: {2, 3, 5 }
45+
46+
47+
48+ 2)
49+ 0
50+
51+ Returns: {7, 10, -70 }
52+
53+ Note that 0 and 1 are not allowed, thus a result like 0 * 0 + 0 would be incorrect.
54+
55+ 3)
56+ 500
57+
58+ Returns: {-400, -3, -700 }
59+
60+ Some or all of the returned numbers may be negative.
61+
62+ 4)
63+ 2
64+
65+ Returns: {2, 2, -2 }
66+
67+
68+
69+ 5)
70+ 5
71+
72+ Returns: {5, 2, -5 }
73+
74+
You can’t perform that action at this time.
0 commit comments