Skip to content

Commit c1fa1cb

Browse files
Add files via upload
1 parent f3046e9 commit c1fa1cb

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
If there are an odd number of odd numbers, then the sum is odd
2+
3+
If there are an even number of odd numbers and there is at least one even number,
4+
we can make the even number odd and get an odd sum
5+
6+
If there are no odd numbers, the sum will always be even
7+
8+
-----
9+
10+
void solve()
11+
{
12+
int no_of_elements;
13+
cin >> no_of_elements;
14+
15+
int odd_count = 0;
16+
for(int i = 1; i <= no_of_elements; i++)
17+
{
18+
int x;
19+
cin >> x;
20+
21+
odd_count += (x%2 == 1);
22+
}
23+
24+
cout << ((odd_count%2 == 1) || (odd_count > 0 && odd_count%2 == 0 && odd_count != no_of_elements) ? "YES\n" : "NO\n");
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
We can just be greedy and simulate the problem.
2+
3+
We will use 10(X/10) coins. We will get (X/10) extra coins
4+
5+
We will repeat the same with the (X/10) coins that we have now and keep doing this until we have at least 10 coins
6+
7+
8+
-----
9+
10+
void solve()
11+
{
12+
int budget;
13+
cin >> budget;
14+
15+
long long no_of_moves = 0;
16+
while(budget >= 10)
17+
{
18+
long long move = budget/10;
19+
20+
budget = budget - 10*move + move;
21+
22+
no_of_moves += 10*move;
23+
}
24+
25+
no_of_moves += budget;
26+
cout << no_of_moves << "\n";
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
We will keep track of (x, y) at every stage.
2+
3+
Suppose we have done i steps and are at (x, y) and we were at (x, y) at step p too.
4+
5+
Then, [p + 1, i] is a substring we can remove.
6+
7+
For every i, we will check which was the last point at which we were at (x, y)
8+
9+
Accordingly, we will check the size of the segment [L, R] and check if it is the smallest segment we have encountered so far
10+
11+
-----
12+
13+
void solve()
14+
{
15+
int length;
16+
string S;
17+
cin >> length >> S;
18+
19+
map <pair <int, int>, int> position;
20+
int minimum_distance = length + 1, left = 0, right = length + 1;
21+
22+
position[make_pair(0, 0)] = -1;
23+
for(int x = 0, y = 0, i = 0; i < length; i++)
24+
{
25+
switch(S[i])
26+
{
27+
case 'L' : x++; break;
28+
case 'R' : x--; break;
29+
case 'U' : y++; break;
30+
case 'D' : y--; break;
31+
}
32+
33+
if(position.count(make_pair(x, y)) != 0)
34+
{
35+
int last_i = position[make_pair(x, y)] + 1;
36+
37+
int distance = (i) - (last_i - 1);
38+
39+
if(distance < minimum_distance)
40+
{
41+
minimum_distance = distance;
42+
left = last_i ; right = i;
43+
}
44+
}
45+
46+
position[make_pair(x, y)] = i;
47+
}
48+
49+
if(minimum_distance > length)
50+
{
51+
cout << "-1\n";
52+
return;
53+
}
54+
55+
cout << left + 1 << " " << right + 1 << "\n";
56+
}

0 commit comments

Comments
 (0)