Skip to content

Commit cad0ced

Browse files
Add files via upload
1 parent 66dde22 commit cad0ced

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Let us take the minimum and maximum elements of this array
2+
3+
A[i] - A[1] + A[n] - A[i] = A[n] - A[i]
4+
5+
-----
6+
7+
void solve()
8+
{
9+
int no_of_elements;
10+
cin >> no_of_elements;
11+
12+
vector <int> A(no_of_elements + 1);
13+
for(int i = 1; i <= no_of_elements; i++)
14+
{
15+
cin >> A[i];
16+
}
17+
18+
int minimum = 1, maximum = 1;
19+
for(int i = 1; i <= no_of_elements; i++)
20+
{
21+
if(A[i] >= A[maximum])
22+
{
23+
maximum = i;
24+
}
25+
else if(A[i] < A[minimum])
26+
{
27+
minimum = i;
28+
}
29+
}
30+
31+
cout << minimum << " " << maximum << "\n";
32+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
Notice that we can never change a 0 or 1
2+
If there is a 0 or 1, it will remain 0 or 1 throughout all the operations that we do.
3+
4+
The next observation to make is that we can set every element = 0
5+
We can start with the largest element and make all elements = 0 by choosing x = max of the current array
6+
7+
This works, except when there is a 1 in the array
8+
9+
-----
10+
11+
When there is a 1 in the array, we can follow a similar algorithm and set all elements = 1,
12+
by choosing X = Array Max - 1 at each step
13+
14+
The only exception is when we have 2 consecutive elements.
15+
When we have 2 consecutive elements, we can never make both elements = 1
16+
17+
We will finish at a situation where m = 1 and m + 1 = 2 or m = 0 and m + 1 = 2
18+
In both cases, we can not make 0 or 2 = 1 by any operation
19+
20+
Now, it is impossible.
21+
22+
------
23+
24+
It is impossible when there is a 1 in the array and there are consecutive elements in the array
25+
26+
Otherwise, we can make all elements = 0 or 1 depending on whether the array has a 1 or not.
27+
28+
-----
29+
30+
void solve()
31+
{
32+
int no_of_elements;
33+
cin >> no_of_elements;
34+
35+
vector <long long> A(no_of_elements + 1);
36+
for(int i = 1; i <= no_of_elements; i++)
37+
{
38+
cin >> A[i];
39+
}
40+
41+
vector <int> present(3, false);
42+
for(int i = 1; i <= no_of_elements; i++)
43+
{
44+
if(A[i] <= 2)
45+
{
46+
present[A[i]] = true;
47+
}
48+
}
49+
50+
int possible = true;
51+
if(present[1])
52+
{
53+
sort(A.begin(), A.end());
54+
55+
for(int i = 1; i + 1 <= no_of_elements; i++)
56+
{
57+
if(A[i] + 1 == A[i + 1])
58+
{
59+
possible = false;
60+
break;
61+
}
62+
}
63+
}
64+
65+
cout << (possible ? "Yes" : "No") << "\n";
66+
}

0 commit comments

Comments
 (0)