Easy Solution Using Sets.
Solution Link: CodeChef: Practical coding for everyone
int n;
cin>>n;
set<int> S;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
S.insert(x);
}
for(auto s:S)
{
if(S.count(s+1)||S.count(s-1)) continue;
else S.insert(s+1);
}
cout<<S.size()-n<<endl;
I know that it is wrong .
I just want to know that is there is a chance that I can correct it(Can you please tell). Or try some another approach.
@brijesh293 Yes, you can.
Just need to check the previous tree can make the current tree beautiful or not…for every tree in rangei=1 to n-1. If we need to do so, then many changes require to do. Therefor, we just skip the beautiful trees using while loop, that’s require few changes.
Your AC Code
Two lines added and one condition added to else. Please follow the comment to check out the correction:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n;
vector<int> v;
for (int i = 0; i < n; ++i) {
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
int c = 0, i;
v.push_back(-1);
for (i = 0; i < n - 1; ++i) {
if (i > 0) // Added
while (i < n and (v[i] - v[i - 1]) == 1) i++; //Added
if ((v[i + 1] - v[i]) == 1)
++i;
else if ((v[i + 1] - v[i]) == 2) {
++c; ++i;
} else if (i < n) { // condition (i<n) added
++c;
}
}
if (i == n - 1)
if (v[i] - v[i - 1] != 1) c++;
cout << c << endl;
}
return 0;
}
Here is the AC submission
@anon71312354 Hmm easy enough BUT not time+space optimized. If the constraints changed a few, then you get a TLE.
“If the sum of N over all test cases does not exceed 10^6” increased to 10^7 and provide an array with 0 (Zero) beautiful tree then your code will get TLE. Here is the efficient submission and Editorial
@rohit_mahale You should initiate a[n] index with a value that never make the last tree beautiful, for ex.- a[n] = 1e11;
Here is your AC Solution Just added A[n] = 1e11
Clarification
If the array a = {1, 2, 3,5} and if unluckily a[n] == 6 Then it’s conclude as last tree is a beautiful BUT it’s really NOT. That’s why you need to initialize a[n] with a value that constrains Ai\pm 1 never hold.
@otutsukihyuuga Just for the while loop.
You should include #include <bits/stdc++.h> header and replace whole while loop with std:sort(a, a+n) Then your solution will be AC. Here is your AC Solution
ohk thanks man!!
will take care of this in future as well 
So I just needed efficient sorting algorithm right?
But as far as I know you can write your code in a single line unless there are multiple statements in a block. Then why is it giving wrong answer? And if there was an issue with the syntax then it wouldn’t have compiled.
i think i have used a very simple approach by looking at the solution others posted.
just keep a pointer(l) of where you have planted last tree. which is array[0]+1 initially…and a counter of how many extra tree planted upto now.
now for every index there are 3 conditions possible.
- either l==array(i) which means that i have planted a extra tree on the same place so counter–
- if (array(i) - l) >1 which means that there is more than 1 difference between last planed tree and current tree so plant a tree just after array(i). and update l
- else update l as array(i).
that’s it
print counter.
Can anyone please tell me what’s wrong in my code
https://www.codechef.com/viewsolution/28007619
I know i can replace the if conditions by increasing length of array by placing some large values
At index 0 and n but i want to know why this code is not working
@super_saiyan1 Yes, You are right BUT else statement works for the nearest if condition (If you ignore the bracket for single statement)
Can anybody explain why the Editorialist code has arr[1<<20] ?
https://www.codechef.com/viewsolution/30711113
please help i use map and add the plants if not filled at -1 or at +1 please help
I am facing problem with my code.My code is partially correct please help…
code link-CodeChef: Practical coding for everyone
I am checking the boundaries (0 and n-1) instead of making the array size =(n+2),but getting WA.
can i get help in my code:
https://www.codechef.com/viewsolution/32203570
In the solution code https://www.codechef.com/viewsolution/33891833 . I have used a array instead of using map.
Can anyone explain why it is getting partially accepted?