|
| 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